The dust-to-stellar mass ratio (Mdust/M⋆) is a crucial yet poorly constrained quantity to understand the production mechanisms of dust, metals and stars in galaxy evolution. In this work we explore and interpret the nature of Mdust/M⋆ in 300 massive (M⋆>1010M⊙), dusty star-forming galaxies detected with ALMA up to z≈5. We find that Mdust/M⋆ evolves with redshift, stellar mass, specific SFR and integrated dust size, differently for main sequence and starburst galaxies. In both galaxy populations Mdust/M⋆ rises until z∼2 followed by a roughly flat trend towards higher redshifts. We show that the inverse relation between Mdust/M⋆ and M⋆ holds up to z≈5 and can be interpreted as an evolutionary transition from early to late starburst phases. We demonstrate that Mdust/M⋆ in starbursts mirrors the increase in molecular gas fraction with redshift, and is enhanced in objects with the most compact dusty star-formation. The state-of-the-art cosmological simulation SIMBA broadly matches the evolution of Mdust/M⋆ in main sequence galaxies, but underestimates it in starbursts. The latter is found to be linked to lower gas-phase metallicities and longer dust growth timescales relative to data. Our data are well reproduced by analytical model that includes recipes for rapid metal enrichment, strongly suggesting that high Mdust/M⋆ is due to fast grain growth in metal enriched ISM. Our work highlights multifold benefits of using Mdust/M⋆ as a diagnostic tool for: (1) separating main sequence and starburst galaxies until z∼5; (2) probing the evolutionary phases of dusty galaxies, and (3) refining the treatment of dust life cycle in simulations.