  We calculate the leptonic decay constants of heavy-light pseudoscalar mesons
with charm and bottom quarks in lattice quantum chromodynamics on four-flavor
QCD gauge-field configurations with dynamical $u$, $d$, $s$, and $c$ quarks. We
analyze over twenty isospin-symmetric ensembles with six lattice spacings down
to $a\approx 0.03$~fm and several values of the light-quark mass down to the
physical value $\frac{1}{2}(m_u+m_d)$. We employ the highly-improved
staggered-quark (HISQ) action for the sea and valence quarks; on the finest
lattice spacings, discretization errors are sufficiently small that we can
calculate the $B$-meson decay constants with the HISQ action for the first time
directly at the physical $b$-quark mass. We obtain the most precise
determinations to-date of the $D$- and $B$-meson decay constants and their
ratios, $f_{D^+} = 212.7(0.6)$~MeV, $f_{D_s} = 249.9(0.4)$~MeV,
$f_{D_s}/f_{D^+} = 1.1749(16)$, $f_{B^+} = 189.4 (1.4)$~MeV, $f_{B_s} =
230.7(1.3)$~MeV, $f_{B_s}/f_{B^+} = 1.2180(47)$, where the errors include
statistical and all systematic uncertainties. Our results for the $B$-meson
decay constants are three times more precise than the previous best lattice-QCD
calculations, and bring the QCD errors in the Standard-Model predictions for
the rare leptonic decays $\overline{\mathcal{B}}(B_s \to \mu^+\mu^-) = 3.64(11)
\times 10^{-9}$, $\overline{\mathcal{B}}(B^0 \to \mu^+\mu^-) = 1.00(3) \times
10^{-10}$, and $\overline{\mathcal{B}}(B^0 \to
\mu^+\mu^-)/\overline{\mathcal{B}}(B_s \to \mu^+\mu^-) = 0.0273(9)$ to well
below other sources of uncertainty. As a byproduct of our analysis, we also
update our previously published results for the light-quark-mass ratios and the
scale-setting quantities $f_{p4s}$, $M_{p4s}$, and $R_{p4s}$. We obtain the
most precise lattice-QCD determination to date of the ratio $f_{K^+}/f_{\pi^+}
= 1.1950(^{+16}_{-23})$~MeV.
