Suppose a lab analysis of a white powder finds 40.0% carbon, 6.7% hydrogen and 53.3% oxygen by mass. Converting to moles gives a ratio of 1 C : 2 H : 1 O, so the empirical formula is CH2O. But that ratio fits many molecules: formaldehyde CH2O, acetic acid C2H4O2, lactic acid C3H6O3 and glucose C6H12O6 all have exactly those percentages. Composition tells you the proportions, not the size of the molecule.
The fact that breaks the tie is the molar mass, which comes from a separate measurement such as a mass spectrometer. One CH2O unit has a mass of 12.01+2(1.008)+16.00=30.03 g/mol. If the compound's molar mass is 180 g/mol, each molecule must hold 180÷30.03≈6 units, so the molecular formula is C6H12O6. The multiplier n is always a whole number, because a molecule cannot contain part of an atom; a result like 2.5 means an error earlier in the work.
Not every formula is a molecular formula. NaCl looks like one, but table salt is a lattice of Na+ and Cl− ions with no separate molecules, so NaCl is a formula unit and the ratio is the whole story. You can usually tell from the elements: a metal combined with a nonmetal forms an ionic lattice, while nonmetals bonded to each other form molecules with a definite atom count.
Think of it like this. An empirical formula is like a recipe card that says 1 cup of flour for every 2 eggs; the molecular formula is the batch you actually made, say 6 cups and 12 eggs. The molar mass is the scale reading that reveals how many times the recipe was multiplied. Unlike baking, the multiplier must be a whole number.
Core idea. The molecular formula is the empirical formula multiplied by a whole number n, where n is the compound's molar mass divided by the empirical formula mass. Composition data fix the ratio of atoms; only the molar mass fixes how many atoms one molecule holds.