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 certain wavelengths of light. The rest of the light will pass right through the cloud. So in this case astronomers would see an absorption line spectrum, where certain wavelengths of light 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.
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 is the first of a 3-part series that is a very good basic description of how spectroscopy works and how it is used in astronomy:
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 nearby supernova:
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 emit wavelengths compressed slightly towards the blue wavelengths (called blueshift) and objects moving away will emit wavelengths slightly stretched towards the red wavelengths (called redshift). This is because of the Doppler effect. This principle can be used to study the motion of galaxies, to find stars that are in binary star systems, or stars with planets orbiting them.
For more information on how spectroscopy works and what astronomers use spectroscopy for, please consider watching this fascinating 17 minute talk by Garik Israelian about what astronomers are currently discovering using spectroscopy:
An excellent simulation of electrons absorbing photons of various wavelengths.
This animation from PhET Interactive Simulations illustrates how electrons absorb and emit photons.