Spectra
Spectroscopy uses the principle first suggested by Isaac Newton, that white light is a combination of many different colors of light, and a prism can be used to separate white light into a rainbow called a spectrum (plural: spectra). In 1814 Joseph von Fraunhofer shone sunlight through a prism, then magnified the spectrum. He was surprised to see over 600 fine, dark lines in the Sun's spectrum. These lines are called spectral lines.
About 50 years later, Robert Bunsen and Gustav Kirchhoff used a gas burner invented by Bunsen (a Bunsen burner) which produced a clean flame with no color of its own. They added different substances to the flame, then passed the emitted light through a prism. They soon discovered that each chemical element produces its own unique pattern of spectral lines.
When a substance is very hot, it will emit light at all wavelengths in a continuous spectrum. If a continuous spectrum of light passes through a gas cloud, atoms in that gas will absorb only photons of certain wavelengths. The rest of the light will pass right through the cloud. So in this case astronomers would see an absorption line spectrum, where certain wavelenths have been absorbed by the gas. When astronomers view a spectrum from a gas that does not have a light source behind it, they will see the wavelengths that the gas emits. This is called an emission line spectrum. Absorption lines and emission lines will be in the same place for the same gas. These lines can be explained by the behavior of electrons in an atom.
Electron Energy Levels
To understand the spectral lines they were observing, scientists needed to develop a better model of the atom than they had at the time. Niels Bohr studied the spectra of hydrogen and was the first to come up with an explanation that fit with observations. He proposed that electrons in an atom can only exist at certain distances or energy levels from the nucleus, and that because they can only exist in certain levels, electrons around the nucleus of an atom can only absorb certain wavelengths of light, which give them enough energy to move further away from the nucleus. An electron will usually stay in a higher energy level for a very short time (10-8 seconds) before dropping down to a lower energy level and emitting a photon of the same energy it absorbed. This video from Penn State University illustrates how electrons move between different energy levels.
Spectrographs
Astronomers use spectrographs attached to telescopes to view the spectral lines of stars and other celestial objects. A spectrograph combines either a prism or diffraction grating to spread the light from a source into its spectrum. It then has a detector, usually a CCD, to record the spectrum. Astronomers use computers to analyze the spectra and create graphs such as this one from a Wolf Rayet Star:

Applications of Spectroscopy
Astronomers can compare the spectral lines they observe to the spectral lines of known elements to learn about the chemical composition and temperature of astronomical objects. They can also study the motion of astronomical objects, because objects that are moving towards an observer will have wavelengths shifted slightly towards the blue wavelengths (called blueshift) and objects moving away will have wavelengths slightly shifted towards the red wavelengths (called redshift). This is because of the Doppler effect. This principle can also be used to find stars that are binary star systems, or stars with planets orbiting them. In these cases, the star will have blueshift some of the time and redshift some of the time because the gravitational pull of the orbiting object will make the star move towards the Earth some of the time and away from the Earth some of the time.
More information
For more information on how spectroscopy works and what astronomers use spectroscopy for, please consider watching the following videos:
This is the first of a 3-part series that is a very good basic description of the use of spectroscopy in astronomy:
A fascinating 17 minute talk by Garik Israelian about what astronomers are currently discovering using spectroscopy:
Other Resources:
Very inexpensive spectrograph kits available from the Solar Center at Stanford University.
An excellent simulation of electrons absorbing photons of various wavelengths.
This animation from PhET Interactive Simulations illustrates how electrons absorb and emit photons.
- Go to the Hydrogen atom simulation.
- Click on the "Run Now!" button to start the simulation.
- In the simulation, use the selector in the top left to choose "Prediction."
- Select "Bohr."
- Turn on the power to the electron gun (click the red button on the drawing) and observe the simulation.