O2 is not a compound; it is a molecule because it contains only one element. To be considered a compound, a substance must meet all of the following criteria: contain at least two elements, be a pure substance, have chemically combined elements and always have the same component ratio.
H2O is an example of a compound, meeting all of the criteria. It contains two elements: hydrogen and oxygen. H2O has different properties than the two elements it consists of, so it is a pure substance. The elements cannot be separated by physical means, and there will always be two hydrogen elements for every oxygen element.