Between the particles of a substance there are forces of attraction. In a solid they hold the particles in their regular arrangement. The state of a substance depends on how much energy the particles have compared with the strength of these forces.
When a solid is heated, its particles gain energy and vibrate more. At the melting point they have enough energy to overcome the forces of attraction between them. The regular arrangement breaks down and the particles can move around each other, so the solid becomes a liquid.
When a liquid is heated to its boiling point, the particles gain enough energy to completely overcome the forces between them. They move far apart and move quickly and randomly, so the liquid becomes a gas. The temperature stays constant during a change of state because the energy supplied is used to overcome the forces, not to speed the particles up.
Cooling reverses these changes. When a gas condenses, the particles lose energy and move more slowly, so the forces of attraction pull the particles closer together. When a liquid freezes, the particles lose more energy and settle into a regular arrangement in fixed positions.