The sun contains many elements but the most abundant by far is hydrogen. The visible layers (the photosphere and the chromosphere) is the only part of the sun that is cool enough for hydrogen to exist in it's atomic form and it is here that we can see the absorption and emission spectra for hydrogen.
It is helpful to think of a hydrogen atom as a small 'solar system' with the heavy nucleus as the 'sun' in the middle. This particular solar system has only one planet orbiting - ie a single electron. Due to the laws of quantum physics, this electron can only orbit the nucleus in specific orbits which are given a number n.
When electrons jump from the lower to the higher number orbits, they absorb a particular amount of energy and we can observe the absorption spectrum. When they fall back again they release the same amount of energy and we can observe the emission spectrum. The amount of energy absorbed or released in this way can be mathematically directly related to the wavelength at which we see the absorption and emission lines on the spectrum.
Hydrogen can absorb and emit in the ultraviolet region of the spectrum (the Lyman series) but the emissions and absorptions we see in the visible part of the spectrum are the Balmer series and occur when electrons jump from and fall to the n=2 orbit.