The cosmic-ray fluxes of electrons and positrons ($e^{\pm}$) are measured with high precision by the space-borne particle spectrometer AMS-02. To infer a precise interpretation of the production processes for $e^{\pm}$ in our Galaxy, it is necessary to have an accurate description of the secondary component, produced by the interaction of cosmic-ray proton and helium with the interstellar medium atoms. We determine new analytical functions of the Lorentz invariant cross section for the production of $\pi^\pm$ and $K^\pm$ by fitting data from collider experiments. We also evaluate the invariant cross sections for several other channels, involving for example hyperon decays, contributing at the few \% level on the total cross section. For all these particles, the relevant 2 and 3 body decay channels are implemented, with the polarized $\mu^\pm$ decay computed with next-to-leading order corrections. The cross section for scattering of nuclei heavier than protons is modeled by fitting data on $p+C$ collisions. The total differential cross section $d\sigma/dT_{e^\pm}(p+p\rightarrow e^\pm+X)$ is predicted from 10 MeV up to 10 TeV of $e^\pm$ energy with an uncertainty of about 5-7\% in the energies relevant for AMS-02 positron flux, thus dramatically reducing the precision of the theoretical model with respect to the state of the art. Finally, we provide a prediction for the secondary Galactic $e^\pm$ source spectrum with an uncertainty of the same level. As a service for the scientific community, we provide numerical tables and a script to calculate energy-differential cross sections.