In more detail
Multiply each element's atomic weight by how many times it appears in the formula and add them up, using values such as H 1.008, C 12.011, O 15.999, Na 22.990 and Cl 35.45. The result in g/mol has the same number as the mass of one molecule in daltons. Standard atomic weights are averages over the natural mix of isotopes, which is why mass spectrometry, which sees individual isotopes, reports slightly different masses. For ionic compounds such as NaCl, which form no separate molecules, the same sum is called the formula weight.
Why it matters
Molecular weight converts grams to moles, the first step in any concentration or reaction calculation. Using the wrong form of a chemical shifts every result: copper sulfate pentahydrate (CuSO₄·5H₂O) is 249.68 g/mol but the anhydrous salt is 159.60 g/mol, so weighing one for the other is a 56% error.
Example
H₂O: 2 × 1.008 + 15.999 = 18.015 g/mol
C₆H₁₂O₆: 6 × 12.011 + 12 × 1.008 + 6 × 15.999 = 180.156 g/mol
90 g of glucose = 90 / 180.156 = 0.4996 mol
Common confusion
Molecular weight is a property of a substance (g/mol); Molarity is the concentration of a solution (mol/L). Both involve moles, so the units are easy to mix up.