A search is presented for a charged Higgs boson heavier than the top quark, produced in association with a top quark, or with a top and a bottom quark, and decaying into a top-bottom quark-antiquark pair. The search is performed using proton-proton collision data collected by the CMS experiment at the LHC at a center-of-mass energy of 13 TeV, corresponding to an integrated luminosity of 35.9 fb−1. Events are selected by the presence of a single isolated charged lepton (electron or muon) or an opposite-sign dilepton (electron or muon) pair, categorized according to the jet multiplicity and the number of jets identified as originating from b quarks. Multivariate analysis techniques are used to enhance the discrimination between signal and background in each category. The data are compatible with the standard model, and 95% confidence level upper limits of 9.6–0.01 pb are set on the charged Higgs boson production cross section times branching fraction to a top-bottom quark-antiquark pair, for charged Higgs boson mass hypotheses ranging from 200 GeV to 3 TeV. The upper limits are interpreted in different minimal supersymmetric extensions of the standard model. [Figure not available: see fulltext.]