In summary
Like a steam engine or an internal combustion car engine, a Stirling engine converts heat energy to mechanical energy (work) by repeating a series of basic operations, known as its cycle. Let's consider a simplified displacer-type Stirling engine. It's actually quite confusing and hard to figure out until you realize that what's happening is that the two pistons are repeatedly changing the volume of the cylinder, so the gas inside is alternately expanding and contracting and, in between, moving from the hot side of the cylinder to the cool side and back again. The dark-blue work piston's job is to use energy from the expansion of the gas to drive the machine the engine is powering, then compress the gas so the cycle can repeat. The light-blue displacement piston's job is to move the gas from the hot side of the cylinder (on the left) to the cold side (on the right) and back. Together, the pistons ensure that heat energy is repeatedly being moved from the source to the sink and converted into useful mechanical work.