To put it simply: mass is a function of the number of protons and neutrons in the nucleus of an atom, and that number is unique to a substance.
One thing to keep in mind is the existence of isotopes, substances which have the same number of protons, but different numbers of neutrons. The more neutrons, the heavier. The compound known as "heavy water" is water formed with an Oxygen atom and two atoms of deuterium, which is a single proton with two neutrons, an isotope of Hydrogen.