Electrons are bound to a nucleus in quantised energy levels. As they gain and lose energy they transition between this energy levels by emitting photons. Emitted photons have energies equal to the change in energy of the electron due to the conservation of energy. Since every different element's nucleus has different energy levels, they emit photons of specific energies and wavelengths, providing a unique spectrum of light. By comparing the frequencies of light emitted by stars to known frequencies emitted by elements found experimentally on Earth, the elements present in stars can then be identified.