Once you understand the energy level diagram, it quickly becomes obvious that atoms can only emit certain frequencies of photons, correlating to the difference between energy levels as an electron falls from a higher energy state to a lower energy state. In similar fashion, electrons can only absorb photons with energy equal to the difference in energy levels as the electron jumps from a lower to a higher energy state. This leads to unique atomic spectra of emitted radiation for each element.
An object that is heated to the point where it glows (incandescence) emits a continuous energy spectrum, described as blackbody radiation.
If a gas-discharge lamp is made from mercury vapor, the mercury vapor is made to emit light by application of a high electrical potential. The light emitted by the mercury vapor is created by electrons in higher energy states falling to lower energy states, therefore the photons emitted correspond directly in wavelength to the difference in energy levels of the electrons. This creates a unique spectrum of frequencies which can be observed by separating the colors using a prism, known as an emission spectrum. By analyzing the emission spectra of various objects, scientists can determine the composition of those objects.
In similar fashion, if light of all colors is shone through a cold gas, the gas will only absorb the frequencies corresponding to photon energies exactly equal to the difference between the gas’s atomic energy levels. This creates a spectrum with all colors except those absorbed by the gas, known as an absorption spectrum.