We study the dust content of galaxies from z = 0 to z = 9 in semi-analytic models of galaxy formation that include new recipes to track the production and destruction of dust. We include condensation of dust in stellar ejecta, the growth of dust in the interstellar medium (ISM), the destruction of dust by supernovae and in the hot halo, and dusty winds and inflows. The rate of dust growth in the ISM depends on the metallicity and density of molecular clouds. Our fiducial model reproduces the relation between dust mass and stellar mass from z = 0 to z = 7, the number density of galaxies with dust masses less than 10 8.3 M , and the cosmic density of dust at z = 0. The model accounts for the double power-law trend between dust-togas (DTG) ratio and gas-phase metallicity of local galaxies and the relation between DTG ratio and stellar mass. The dominant mode of dust formation is dust growth in the ISM, except for galaxies with M * < 10 7 M , where condensation of dust in supernova ejecta dominates. The dust-to-metal ratio of galaxies depends on the gasphase metallicity, unlike what is typically assumed in cosmological simulations. Model variants including higher condensation efficiencies, a fixed timescale for dust growth in the ISM, or no growth at all reproduce some of the observed constraints, but fail to simultaneously reproduce the shape of dust scaling relations and the dust mass of high-redshift galaxies.