Cosmic Histories of Stars, Gas, Heavy Elements, and Dust in Galaxies