We implement a detailed dust model into the L-Galaxies semi-analytical model which includes: injection of dust by type II and type Ia supernovae (SNe) and AGB stars; grain growth in molecular clouds; and destruction due to supernova-induced shocks, star formation, and reheating. Our grain growth model follows the dust content in molecular clouds and the inter-cloud medium separately, and allows growth only on pre-existing dust grains. At early times, this can make a significant difference to the dust growth rate. Above $zsim8$, type II SNe are the primary source of dust, whereas below $zsim8$, grain growth in molecular clouds dominates, with the total dust content being dominated by the latter below $zsim6$. However, the detailed history of galaxy formation is important for determining the dust content of any individual galaxy. We introduce a fit to the dust-to-metal (DTM) ratio as a function of metallicity and age, which can be used to deduce the DTM ratio of galaxies at any redshift. At $zlesssim3$, we find a fairly flat mean relation between metallicity and the DTM, and a positive correlation between metallicity and the dust-to-gas (DTG) ratio, in good agreement with the shape and normalisation of the observed relations. We also match the normalisation of the observed stellar mass -- dust mass relation over the redshift range of $0-4$, and to the dust mass function at $z=0$. Our results are important in interpreting observations on the dust content of galaxies across cosmic time, particularly so at high redshift.