Finding the molecular weight of a compound in the mass spectrum can provide valuable evidence for a structure. This information is especially valuable if we already have an idea of what the structure might be. In that case, we could simply compare the observed molecular weight to the one that was expected.
If we do not know what the structure is, knowing the molecular formula does limit the possibilities. There will be only so many combinations of atoms that add up to a certain molecular weight. In combination with other information, such as NMR and IR spectroscopy, we might be able to work out a formula that fits the observed molecular weight.
There are mass spectrometers that are designed to measure the value of m/z so accurately that the possible molecular formulae decrease to only a handful. These "high resolution" mass spectrometers rely on the fact that the atomic masses of different atoms are not exact integers. The atomic mass of a 1H atom (the isotope that just has a proton in the nucleus, and no neutron) is actually 1.0078 amu, not 1.0000 amu. The atomic mass of a 10B atom is actually close to 10.0129 amu, rather than the 10.0000 amu that you might expect. These are the masses of individual atoms, not average masses that take into account different isotopes. A high resolution mass spectrometer will measure the mass of the molecule so accurately that it can compute the very few combinations of atoms that could combine to produce that mass.
Once we have a formula, we actually get a great deal of information automatically. One of the most important pieces is "units of unsaturation" or "degrees of unsaturation" (DU). The DU is the result of a formal comparison of the C/H ratio in the compound to that in a normal alkane. In a normal alkane, the formula is always CnH2n+2. If you picture a long hydrocarbon chain, there will be two hydrogens on each carbon along the chain, plus one more hydrogen at either end of the chain. However, an alkene contains one pi bond, and at the site of that pi bond there are two hydrogen atoms missing from that alkane formula. A simple alkene always has the formula CnH2n. That missing pair of hydrogens in the formula is called a degree of unsturation.
The same thing also happens to the formula if there is a ring present. One DU can correspond to the presence of a double bond or a ring. If DU=2, there may be two double bonds, two rings, or one of each.
If there are oxygen atoms present in the formula, we can just ignore them and pay attention to the hydrocarbon part. Conceptually, because oxygen forms two bonds, we can think of it as squeezing in between any two atoms in a hydrocarbon structure to form a new compound. The ratio of carbon to hydrogen is unchanged. If there is a degree of unsturation in a formula containing oxygen, it simply suggests the presence of a ring or a double bond, just like in a hydrocarbon.
Suppose the formula is C9H10O2. We can ignore the oxygens and look at the C9H10. If this were a saturated hydrocarbon with nine carbons, its formula would be C9H20 (since 2 x 9 + 2 = 20). We are missing five pairs of hydrogens, so DU = 5. That is a lot. However, if we have one benzene in the structure, that would account for three double bonds and one ring all at once. That is four degrees of unsaturation. An additional carbonyl or other double would bring the number up to the required five. If we had not yet arrived at the idea of a benzene ring, this comparison might make us think of it.
Sometimes, if there are other atoms present, we need to adjust the formula to take them into account. For example, any time a halogen is found in the structure, it conceptually replaces a hydrogen atom. In order for a halogen to be found in the structure, there would have to be one fewer hydrogen atoms in order to open up a spot for the halogen. To adjust for the presence of a halogen, we need to add one hydrogen into the formula, then compare it to the standard alkane formula.
Nitrogen, on the other hand, has three bonds. Unlike oxygen, if we squeeze it in between two other atoms, it still needs one extra bond. It always brings an extra hydrogen into the formula. To adjust for the presence of nitrogen, we need to subtract one H from the formula, then compare it to the standard alkane formula.