Spectral lines are caused by electrons within atoms being excited up energies levels by absorbing specific frequencies of light (this produces absorption lines- dark lines in an otherwise continuous spectrum) or when electrons drop down a energy level, emitting light of a specific frequency as they do so. They can be used to identify elements that are present within stars, as each spectrum is unique to an element. It has been observed by scientists that the exact same spectrum from a specific element that is measured on Earth can be found in light from stars from distant galaxies, except it is moved towards the red end of the electromagnetic spectrum, which means the wavelength of the light has been stretched ater it was emitted from the star and before it was measured on Earth.
This can be explained by the Doppler effect- an effect that shows that any source of waves moving relative to an observer shows an increase in wavelength as it moves away from the observer, and a decrease in wavelength as it moves towards the observer. Therefore, the red shift in light from distant galaxies shows that these galaxies are moving away from Earth.