Step 1 - Understanding a phase transition
During a phase transition, the intermolecular structure of a substance is modified. Let's take the transition from solid to liquid as an example.
In the solid state, the atoms or molecules are very close together in what is called a crystalline structure. When we heat a solid (let's say ice), the molecules start to move faster and faster, thus moving apart from each other.
During this whole process, no temperature raising is observed: all the energy is used to break intermolecular interactions. When they are finally weakened, the molecular are farther apart, which means the substance has reached the liquid state.
Step 2 - The movement of energy
During a phase transition, energy goes into the system (i.e., the substance) in order to allow the weaking of the molecular interactions. Energy can also go out the system, if a liquid turns solid, like freezing water, for example. It depends on the directionality of the phase transition.
So, in short:
solid --> liquid --> gas (energy flows to the system)
gas --> liquid --> solid (energy flows out of the system)