Spectroscopy is a branch of science where atoms and molecules are identified by light emitted by excited electrons dropping to lower energy levels.
The diagram above shows energy levels for an electron in a hydrogen atom. Hydrogen is the simplest atom, consisting of one electron bound to a nucleus of one proton.
The electron typically resides in its ground state, or lowest energy level. If the atom absorbs a photon with an energy of 10.2 electron volts (eV), the electron is raised to its first excited state. It will reside there for a short time and then emit a photon and drop back to ground state.
If the atom absorbs a photon with an energy of 12.1 eV, the electron is raised to the second excited state. It will drop back to ground state by either emitting one photon of energy 12.1 eV or by emitting a photon of 2.1 eV (the difference in energy between the second and first excited states) and then emitting a photon of 10.2 eV.
The excited state energy levels are quantized. That means they are discrete levels. A photon with 12.8 eV, for example, can not be absorbed by a hydrogen atom. 13.6 eV is the ionization energy for a hydrogen atom. This is how much energy it takes to liberate the electron from the nucleus of the atom.
The energy can also be expressed in joules, using the conversion factor between electron volts and joules.
The collection of energies that can be produced by relaxation of excited electrons in hydrogen is called the hydrogen spectrum.
Passing a high voltage through hydrogen gas, as seen in the glass tube of hydrogen above, causes it to glow. A seen through a diffraction grating (like many small prisms), the light separates into distinct colors. The colors can be used to identify the gas present. The atomic spectrum of a gas is unique to the gas, like a fingerprint. This kind of spectrum is called an emission spectrum.
The helium atom has different excited state energies. The helium spectrum, shown above, displays the colors of light given off when excited electrons in helium atoms relax to lower states. By observing the differences in the spectra, helium and hydrogen can be identified. By analyzing starlight, the atoms and molecules present in the star can be identified.
When light passes through a cool gas, the emission spectrum of the light is changed into an absorption spectrum. The specific wavelengths of light in the emission spectrum are scattered in the gas, causing a lack of those colors.
The gas at the surface of a star is not cool, compared to everyday conditions on Earth, but it is still much cooler than the inner part of the star where the light is produced. Starlight exhibits an absorption spectrum that can be used to identify elements and molecules present in the star.
If you took all of the rows of the spectrum shown above and placed them end-to-end, you would recreate the spectrum of the Sun. As you can see, there is very much detail in this spectrum, and a wealth of information about the chemistry of the Sun.