Creating an ordered solid from a disordered liquid releases energy
At the molecular level this is what is happening when you cool something like water.
At first the cooling reduces the average kinetic energy of the water molecules so they slow down. Basically, temperature is the average kinetic energy so removing energy lowers the temperature.
At some point, though, water starts to form ice where the molecules are not wandering about randomly and get rigidly stuck together in ice crystals. But this process releases energy: the ordered ice crystal has a significantly lower energy than water at the same temperature. Converting liquid water to ice releases that energy.
What this means is that when extracting energy from warm water the result is simply a lower temperature. But one you get to 0°C the extra energy extracted is used to convert the liquid to the solid rather than reducing the temperature. Once all the water has solidified, further removing energy reduces the temperature again.
The key is that the process of freezing itself involves energy changes so instead of reducing the temperature, the energy comes from the conversion of liquid too solid.