We present a new statistical method to determine the relationship between the stellar masses of galaxies and the masses of their host dark matter haloes over the entire cosmic history from z~4 to the present. This multi-epoch abundance matching (MEAM) model self-consistently takes into account that satellite galaxies first become satellites at times earlier than they are observed. We employ a redshift-dependent parameterization of the stellar-to-halo mass relation to populate haloes and subhaloes in the Millennium simulations with galaxies, requiring that the observed stellar mass functions at several redshifts be reproduced simultaneously. Using merger trees extracted from the dark matter simulations in combination with MEAM, we predict the average assembly histories of galaxies, separating into star formation within the galaxies (in-situ) and accretion of stars (ex-situ). The peak star formation efficiency decreases with redshift from 23% at z=0 to 9% at z=4 while the corresponding halo mass increases from 10^11.8Modot to 10^12.5Modot. The star formation rate of central galaxies peaks at a redshift which depends on halo mass; for massive haloes this peak is at early cosmic times while for low-mass galaxies the peak has not been reached yet. In haloes similar to that of the Milky-Way about half of the central stellar mass is assembled after z=0.7. In low-mass haloes, the accretion of satellites contributes little to the assembly of their central galaxies, while in massive haloes more than half of the central stellar mass is formed ex-situ with significant accretion of satellites at z<2. We find that our method implies a cosmic star formation history and an evolution of specific star formation rates which are consistent with those inferred directly. We present convenient fitting functions for stellar masses, star formation rates, and accretion rates as functions of halo mass and redshift.