Stars emit a stream of particles called photons. The photons can most easily be observed as electromagnetic radiation over all wavelengths that produces a continuous spectrum.
Gas contained in the star consists of atoms made up of nuclei electrons that circulate around the nucleus on well defined shells. When a photon collides with an electron it can move the electron from its shell to a shell at greater distance from the nucleus if its energy matches the energy required for the process exactly. Photon energy is related to wavelength, so only photons with selected wavelengths can dislocate electrons from their shells. Atoms in which electrons have been moved to an outer shell are said to be in an excited state.
As a result a star emits radiation at all wavelengths except at those where the energy has been used to move electrons between shells. Its spectrum therefore contains gaps ("lines") at well defined wavelengths. If the only gas present is hydrogen the visible part of the spectrum appears like this:
(To show the crowding of the spectral lines towards shorter wavelengths the colour band is shown blue to wavelengths somewhat shorter than those visible to the human eye.)
Similar sets of lines exist in the infrared and in the ultraviolet. Gases of other elements have different spectral lines. This allows the identification of elements that make up a particular star.
The composition of gas clouds can be determined in a similar way. When a source of radiation shines through a cloud of gas its photons interact with the electrons contained in the cloud, turning atoms into an excited state. Excited atoms return to their original unexcited state spontaneously and emit radiation in the process. This emitted radiation is sent into all directions. As a result gas cloud spectra appear in two forms:
Given a (hypothetical) source of pure radiation, its radiation will appear as a continuous spectrum if seen through empty space. (Path 1) If it traverses a gas cloud excitation of the gas atoms will produce dark lines in the spectrum. (Path 2) As the gas atoms return to their unexcited state they emit radiation in all directions; this is seen as a bright line spectrum. (Path 3. The same amount of radiation is also emitted along path 2; but because energy is emitted in all directions the percentage emitted in the direction of path 2 is not sufficient to fill the dark lines with energy again.)
In addition to enabling astronomers to determine the elements in gas clouds and stars, spectroscopy can also help to determine the speed with which galaxies move through space:
When a source of waves moves towards an observer the waves are compressed (ie the wavelength appears shorter); when it moves away from an observer the waves are dilated (ie the wavelength appears longer). The effect, known as the Doppler effect after Christian Doppler (1803 - 1853), is well known from acoustic waves: The pitch of a train whistle appears to go from high to low as the train passes because more waves arrive at the observer's ear per unit time during the train's approach than when it moves away.
In spectroscopy the Doppler effect is observed as a shift of the spectral lines towards shorter wavelengths if the radiation emitting body moves towards the observer (blueshift) and towards longer wavelengths if it moves away (redshift). Except for a few star systems in the immediate vicinity of the solar system, which belong to the same galaxy and move towards us, all galaxies and their stars exhibit redshift, which means that they move away from us.