Many Stirling engines have a gas pressure inside them that is nearly equal to the outside atmospheric pressure. There is a fixed mass of gas, typically air, helium or hydrogen. When you heat the outside of the engine the gas expands and pushes the piston out. When you cool it, the gas compresses and the piston is pushed back in by the outside atmospheric pressure. This converts heat energy into mechanical energy or work.
But heating the entire engine then cooling it is not efficient. Because it would require the heating and cooling source to move in and out of position over and over again. So there needs to be a way to heat and cool the engine at the same time. This is done by moving or cycling the gas inside the engine from the hot side to the cool side.
A displacer mechanically moves the gas between a heated location and a cooled location. The displacer is a light weight piston that does not come into contact with the inside of the Stirling engine. The gas can move along the side of the displacer. It moves back and forth taking up space inside the engine displacing the gas from side to side.
When the displacer is on the cool side the gas is pushed to the hot side and it is expanded. When the displacer is on the hot side the gas is pushed to the cool side and it is compressed. This is a simplified explanation of the Stirling cycle which is a type of thermodynamic cycle.
This cyclical action needs to be timed correctly. It can be mechanically timed in many different ways. This is why there are so many types and configurations of heat engines.