Imagine observing the light being emitted from a very hot star. If you can stare at this light unhindered, with no dust and gas between you and the object, you may be lucky enough to observe a perfect blackbody — a rainbow of light described by Wein's Law that will peak at a specific wavelength that matches the object's temperature and that fades away to both the red and the blue in his distinctive curve. This in the distribution of colors is referred to as continuum emission. Such a smooth rainbow is a rare thing to observe, and only appears when intervening material neither absorbs or emits extra light at any particular colors.
Most of the time, however, our view of the stars is interrupted by intervening gas (or even by the star's own atmosphere!). As the starlight passes through this material some of the light may be absorbed, but only at specific wavelengths. Each type of atom and molecule has its own characteristics set of allowed energies; states in which electrons orbit in specific orbitals, or molecules vibrate and rotate in special characteristic ways. If a photon from our hot star interacts with an come or molecule two things can happen; if the light is the wrong color it will continue on through space unhindered, but if that photon has the exact energy needed to boost an electron into a higher energy level or to set a molecule into motion, the photon will be absorbed.
This process of energy removal from a light beam is called absorption. Only the specific energies corresponding to an atom or molecules specific transitions can be absorbed and removed from the beam. A beam of light passing through a cloud of such atoms will have many of these photons removed, thus creating a noticeable dark line of absorption in the object's rainbow spectrum. Since atoms have many different allowed energies, there are various possible upward transitions — for example, level 2 to 3, 2 to 4, 1 to 2, 1 to 3, and so on, in one specific type of atom can lead to a whole series of colors being removed from the rainbow.
For a variety of reasons, and excited atom or molecule doesn't stay excited forever. When an electron gains energy by absorbing a photon it will later return to a lower energy by re-radiating that photon in a completely random direction. This means that for every photon that is absorbed another photon is eventually emitted. So why doesn't the emission fill in the absorption line and leave no visible feature in the spectrum? The answer is that the majority of those later admitted photons are emitted in a completely new directions, going out to visit a different part of space than the original photon would've reached. While some of the photons do fill-in the absorption line a bit, most are cast off to the side, where other observers may see them as forming emission lines against the normal blackness of space.
Planetary nebula possess many of these emission lines. Light from the central star radiates in all directions, and here in the Earth we see a circle of surrounding gas glowing in colors such as green and red. If we look at these nebula using spectrographs instead of our eyes, we find that these colors are admitted into single bright lines — bright emission lines — that match the chemical signatures of hydrogen, oxygen, and other elements. Geometrically, light from the central star that is moving perpendicular to our line of sight is absorbed by surrounding gas and re-radiated directly towards us, essentially making a right-hand or left-hand turn. This radical change in direction times thanks to the re-radiation of photons by excited (and then not excited) atoms.
Since the intervals between energy levels in a given atom are the same whether absorption or emission is occurring, the pattern of emission and absorption lines for a given element is the same. An element can be identified from either its emission lines or its absorption lines.
Much of the discussion of absorption an emission also applies to molecules. As light penetrates through a cloud of molecules in a gas, transitions of electrons create closely spaced energy levels called absorption bands. The molecules in the gas can be identified if the absorption bands themselves can be detected and identified. The absorption bands of a substance have the same wavelength intervals as its emission bands.
It is important to note that sometimes the emission lines don't exactly match the absorption lines in a particular cloud of gas. In some cases, an excited electron may jump many energy levels in one go, and then cascade to a lower energy level in a series of different steps that each emit their own photon.
Spectral lines are extraordinarily useful in astronomy. Thanks to the unique fingerprint of each atom and molecule, it is possible to use spectral lines to identify the composition of different gas cloud, stars, and even planets. If more than one element is present, the pattern is more complex because there are more lines, but the principle is the same. You can deduce the object's composition, even without having a sample! By contrast, the smooth spectrum of thermal radiation, such as we see in hot stars were all of the atoms are ionized and the spectral lines are present, only gives information about the temperature of the object. At the same temperature, a lump of iron or a carbon rod or a cloud of hydrogen all emit the same thermal spectrum. Thus spectral lines are much more useful if we want to determine the chemical composition of a distant object.
If the electrons in an atom are in their lowest possible energy level, that atom cannot produce an emission line. This is because the electrons cannot drop to any lower energy level. An atom in which all electrons are in the lowest possible energy level is said to be in its ground state. An atom in which one or more electrons are in energy levels higher than the lowest available ones is said to be in an excited state. Excited states usually last only a fraction of a second before the electrons decay to the lowest available energy level -- trying to reach equilibrium. Atoms generally need to be disturbed to produce and maintain excited states. This can happen in two ways. Radiation will add energy to a gas and so cause electrons to raise their energy state. In a hot dense gas, the same role can be served by collisions of the atoms or molecules themselves. Heating a gas enclosed within a certain volume increases the velocity of atoms and so increases the probability that they will collide with each other.
Just as thermal emission depends on the temperature of an object, spectral lines also very with temperature. Consider hydrogen, the simplest element. The energy required to raise an electron from the ground state to be free of the atom is the largest amount of energy that can result in a spectral line. Therefore, it corresponds to the shortest wavelength feature we might see. This wavelength is about 90mm, which is in the ultraviolet too blue for our eyes to see. Other electron transitions in hydrogen have smaller energy differences, so they yield redder spectral lines. At visible wavelengths, hydrogen emits a characteristic red line when electrons make a transition from the 3-2 energy levels, and several other transitions in and out of the second energy level also produce lines in the visible spectrum. The specific 3-2 transition line is given the name Hα. Heavier elements have more electrons, and thus more electron energy levels and more possible transitions. This can lead to a denser thicket of spectral lines. But for the most common elements like carbon and nitrogen and oxygen and silicon, the spectral lines fall in the same region of the electromagnetic spectrum. Most of the useful atomic spectral lines fall in the decade of wavelength from 100 nm to 1000 nm (or 0.1 micron to 1 micron). This spans the visible spectral range but also extends to ultraviolet and infrared wavelengths.
Molecules are groupings of atoms that can share their electrons. Therefore, the electron structure in a molecule is more complex than in an atom. The electron's path may take it around two or more nuclei. As a result, the emission line structure of a molecule can be complex. For example, a gas containing water molecules (H2O) has many more emission lines than a gas containing single H and O atoms. The molecule has various ways of responding to a disturbance in addition to having its electrons change energy levels-- for example, it may vibrate like two balls linked with a spring, or it may rotate. As a result, the energy levels from a molecule are vastly more numerous, and the resulting emission or absorption lines blend together into a broader feature called an emission or absorption band. The rest of the story is the same. A given molecule (such as H2O) can produce only certain emission bands, allowing us to identify the molecule in a remote source.
While some spectral features from molecules are seen in the visible spectrum, most transitions tend to fall red-ward of what the human eye can see. Many molecules have shapes that allow them to vibrate and oscillate and transitions between these modes involve less energy than a typical electron transition. Many of the most important spectral features from molecules are found in the infrared and microwave part of the electromagnetic spectrum.
From composition to temperature, spectral lines reveal a lot about the environments in which they are created. While perhaps not as beautiful to look at a celestial images, the emission and absorption lines that pepper the continuum spectrum are in some cases intellectually the most stunning sight an astronomer can look upon.