A search is presented for physics beyond the standard model (SM) using electron or muon pairs with high invariant mass. A data set of proton-proton collisions collected by the CMS experiment at the LHC at root s = 13 TeV from 2016 to 2018 corresponding to a total integrated luminosity of up to 140 fb(-1) is analyzed. No significant deviation is observed with respect to the SM background expectations. Upper limits are presented on the ratio of the product of the production cross section and the branching fraction to dileptons of a new narrow resonance to that of the Z boson. These provide the most stringent lower limits to date on the masses for various spin-1 particles, spin-2 gravitons in the Randall-Sundrum model, as well as spin-1 mediators between the SM and dark matter particles. Lower limits on the ultraviolet cutoff parameter are set both for four-fermion contact interactions and for the Arkani-Hamed, Dimopoulos, and Dvali model with large extra dimensions. Lepton flavor universality is tested at the TeV scale for the first time by comparing the dimuon and dielectron mass spectra. No significant deviation from the SM expectation of unity is observed.