When atoms are heated or subjected to an electric discharge, they absorb energy, which is subsequently emitted as radiation. Actually electrons in an atom or a molecule absorb energy and get excited, they jump from a lower energy level to a higher energy level and they emit radiation when they come back to their original states.
For example, if sodium chloride is heated in the flame of a burner, sodium atoms are produced which gives rise to the characteristic yellow flame color. Similarly, if a discharge is passed through hydrogen gas (H2) at low pressure, some hydrogen atoms (H) are formed, which emit light in the visible region. This phenomenon accounts for the emission spectrum through hydrogen, better known as the hydrogen emission spectrum.