As the universe expands, everything that isn't bound by electromagnetic or other interactions expands with it. This includes the distance between galaxies and the wavelength of CMB photons.
The expansion of the universe was first inferred when Hubble discovered that distant galaxies appear to be receding from us. Imagine that you are the observer sitting at the pole of an expanding Earth. Distant objects appear to be receding at a rate proportional to their distance:
You can convince yourself that there is nothing special about the pole. Despite the fact that we see all distant galaxies flying away from us, we do not live in a particularly special place in the universe!
For our purposes, the most important aspect of the expansion is that the wavelength of the CMB photons also stretches with the expansion. The wavelength is depicted as the wavey line in the above picture. As the wavelength increases, the energy of a photon decreases. For the CMB as a whole, its temperature decreases with the expansion. When the universe doubles in size due to the expansion, the temperature of the CMB is halved until it reaches the rather cool ~3K it is today.
This process is called the cosmological redshift. Recall that red light is less energetic than yellow light.