When you cut an onion, you break cells, releasing their contents. Amino acid sulfoxides form sulfenic acids. Enzymes that were kept separate now are free to mix with the sulfenic acids to produce propanethiol S-oxide, a volatile sulfur compound that wafts upward toward your eyes. This gas reacts with the water in your tears to form sulfuric acid. The sulfuric acid burns, stimulating your eyes to release more tears to wash the irritant away.
Cooking the onion inactivates the enzyme, so while the smell of cooked onions may be strong, it doesn't burn your eyes. Aside from wearing safety goggles or running a fan, you can keep from crying by refrigerating your onion before cutting it (slows reactions and changes the chemistry inside the onion) or by cutting the onion under water.
The sulfur-containing compounds also leave a characteristic odor on your fingers.