Depending on where and when you went to high school, you may remember the teacher wheeling in the 16 mm film projector to play films in class. If you ever picked up a strip of the film and looked at it along the sprocket holes, you would have seen a dark strip with a wavy white line down the middle. This strip holds an optical soundtrack for the film.
The idea behind optical soundtracks is beautifully simple. The white line running down the dark strip varies in width. A lamp shines through one side of the strip and hits a photodetector on the other side. The photodetector's output runs through an amplifier and drives a speaker. The vibrations of the sound are translated into changes in the white strip's width. The photodetector sees and variations in light intensity and reproduces the sound (see this page for details). The optical sound system is easy to add to the film and reliable over the life of the film.
This same optical system was used in theaters playing 35 mm films when talking movies were first released. In the 1950s optical soundtracks were replaced by magnetic recording -- magnetic strips just like those on a cassette tape were applied to the film and sound was recorded on them (see How Cassette Tape Works for details). Magnetic recording allowed for stereo sound and surround sound, and also improved the sound quality. But there were problems with magnetic strip life. Also, the magnetic strips made the films a lot more expensive.
In the 1970s, stereo sound using two optical tracks was made possible by Dolby. This system allows for stereo playback and surround sound, and offered Dolby noise reduction as well. Dolby has been improving the system for many years -- see the links for details.
Here are several interesting links: