The measurement of dielectron production is presented as a function of invariant mass and transverse momentum (pT) at midrapidity (|ye|<0.8) in proton–proton (pp) collisions at a centre-of-mass energy of s=13 TeV. The contributions from light-hadron decays are calculated from their measured cross sections in pp collisions at s=7 TeV or 13 TeV. The remaining continuum stems from correlated semileptonic decays of heavy-flavour hadrons. Fitting the data with templates from two different MC event generators, PYTHIA and POWHEG, the charm and beauty cross sections at midrapidity are extracted for the first time at this collision energy: dσcc¯/dy|y=0=974±138(stat.)±140(syst.)±214(BR)μb and dσbb¯/dy|y=0=79±14(stat.)±11(syst.)±5(BR)μb using PYTHIA simulations and dσcc¯/dy|y=0=1417±184(stat.)±204(syst.)±312(BR)μb and dσbb¯/dy|y=0=48±14(stat.)±7(syst.)±3(BR)μb for POWHEG. These values, whose uncertainties are fully correlated between the two generators, are consistent with extrapolations from lower energies. The different results obtained with POWHEG and PYTHIA imply different kinematic correlations of the heavy-quark pairs in these two generators. Furthermore, comparisons of dielectron spectra in inelastic events and in events collected with a trigger on high charged-particle multiplicities are presented in various pT intervals. The differences are consistent with the already measured scaling of light-hadron and open-charm production at high charged-particle multiplicity as a function of pT. Upper limits for the contribution of virtual direct photons are extracted at 90% confidence level and found to be in agreement with pQCD calculations.