Very well explained by Yggdrasil..
However, just to add few things...
Molecules are subsets of both Elements and Compounds. As O
2 is
molecule of an Element and H
2O is Molecule of Compound.
Now Compounds are both
Ionic and
Covalent in Nature. Ionic Compounds in Solid State exist as Crystals and thus Molecule has no meaning here. However, for e.g, when NaCl is vaporised, its Vapor consists of
NaCl molecules. Here also you can see that NaCl is molecule only because it has atoms from two different elements. Though in this case, we have charged atoms in the form of Cation (Na
+) and Anion (Cl
-).
In case of Covalent Compounds too we have examples like
Polymers where a molecule has no meaning.
Rahul