An electric discharge excites the atoms of a gas. When they fall back to lower energy levels they give out photons with exactly the energy of the drop. Each gas has its own pattern of lines.
Energy levels. The electrons in an atom can have only certain energies, called energy levels. An electron can move up to a higher level if it receives exactly the right amount of energy. In a gas discharge tube the energy comes from the electrons of the electric current: an electron from the current hits an atom and gives up some of its kinetic energy, and the atom is excited.
Photons. An excited atom does not stay excited for long (about 10−8 s). When an electron falls to a lower level, the atom gives out the energy as a photon:
ΔE = Eupper − Elower = h f = h c / λ (λ in nm: ΔE in eV = 1239.84 / λ)
A bigger drop gives a photon of higher energy, higher frequency and shorter wavelength. Since the levels are fixed, the energy drops are fixed too, so the atom can only give out light of certain wavelengths. This is the emission spectrum: a pattern of bright lines. Every element has its own pattern, like a fingerprint, which is how elements are identified in lamps, flames and stars. The glow you see is the mixture of all the lines.
Hydrogen is the simplest case. Its levels follow En = −13.6 eV / n². The four visible lines are the Balmer series, which are all the transitions that end on n = 2 (from n = 3, 4, 5 and 6). The jumps down to n = 1 (the Lyman series) give photons of too much energy to be visible: they are in the ultraviolet. The other gases are more complicated, since they have many electrons and many more levels. The levels for helium and mercury are real. For neon, argon, krypton and xenon the page only draws the energy of each drop, not the actual levels. The visible lines of neon, argon and krypton come from transitions between two excited groups of levels (p to s), not from the ground state. The blue light of xenon comes from xenon ions that have lost an electron.
The bright lines of mercury and the other gases are not equally bright, and the pattern of brightness depends on the pressure and on the current. The relative brightness on this page is an illustration. A mercury lamp gives out most of its energy in the ultraviolet (253.7 nm), which a detector for visible light does not see (a fluorescent tube turns it into white light with a coating).
The picture is not to scale, and the atoms are drawn much larger than their size relative to the tube. Each atom gives out one photon per excitation, and the lines are picked at random according to their relative brightness.
Learn more in the Bohr model lesson. Original idea by Physics High.