We normally expect things to cool by conduction (e.g., eggs heating up in a frying pan on a stove) or convection (e.g., a pot of hot coffee slowly losing thermal energy to the air). This is not useful in the low-density environment of interstellar space because both of these processes require atoms to interact physically. The only feasible way to cool gas is for atoms to radiate thermal energy by emitting photons or by molecules rotating and vibrating away energy.
To understand why atoms can emit light, recall that electrons in atoms can hold only very specific amounts of energy. Electron energy levels are quantized; each atom has a unique set of electron energy levels. The difference between the highest and lowest energy level in a hydrogen atom is 13.6 eV. Electrons cannot stay in a higher energy level for long. They always fall back down to the ground state. The energy the electron loses when it falls often goes into emitting a photon of light. The emitted photon will have the exact same amount of energy that the electron loses.
For the hydrogen atom, the amount of energy released as a photon depends on the beginning and ending energy levels of the electrons:
\begin{equation} E_{photon}=-13.6~\textrm{eV}~\left(\frac{1}{n^2_f}-\frac{1}{n^2_i}\right) \end{equation}Here ni is the number of the starting level and nf is the number of the final level. The ground state is n = 1. An ionized state corresponds to an n of infinity.
For a transition involving the outermost electron in a heavier atom, this equation becomes:
\begin{equation} E_{photon}=(-13.6~\textrm{eV}) {\rm (atomic\ number)}^2 \left(\frac{1}{n^2_f}-\frac{1}{n^2_i}\right) \end{equation}The atomic number is the number of protons in an atom. We see from this equation that the energy released by a photon is proportional to the square of atomic number, which helps heavier atoms cool a gas more efficiently.