All matter is made of particles that are constantly in motion. If matter is warmed up by adding energy, the particles start to move faster and start spread out and bump into each other. If a substance loses energy, the molecules move slower and get closer together, and the substance cools down.
When the liquid in a thermometer heats up, it expands and moves up the tube. When the liquid cools down, it contracts and moves down the tube.