The Carnot cycle is an idealized thermodynamic cycle that describes a perfect heat engine. All real heat engines are imperfect approximations of the theoretical perfection embodied by the Carnot cycle. In the Carnot cycle, not all heat energy is converted into mechanical work, but a large portion is -- the largest portion allowed by physical law.
A heat engine produces work from the temperature differential of two reservoirs. In a combustion engine, one reservoir is the heat created inside the engine (source), and the other is the external surroundings (sink). The heat generated by the source causes the gas inside the cylinder to expand, driving a piston that does work. The thermodynamic state of the gas -- expanding, but with a constant temperature -- is called isothermal.
Eventually the heat source is removed and the gas stops expanding so quickly. If the heat were kept on continuously, the cylinder would explode. The gas begins losing temperature as it reaches its maximum volume and does no more work on the piston. This is called the adiabatic expansion of the gas. Then the piston reverses course, compressing the gas, until it begins to reach a maximum temperature and physical resistance due to the heat it contains, putting the system back into its starting state. Then the cycle begins again.
There are many different types of heat engines. All work on the familiar temperature gradient between a source and a sink. To maximize the efficiency of heat engines, they must be well insulated. In most engines used today, the gas remains a gas throughout the cycle, but in steam engines, a phase change occurs between liquid and gas.
In a perfect Carnot cycle, all four steps happen very slowly, to minimize the entropy, or thermodynamic irreversibility, created by the process. In reality, the steps progress quickly, and entropy is generated, meaning the cycle can't go on forever. The walls of the cylinder degrade, heat from the interior of the engine gets lost to external surroundings, and so on. The Carnot cycle can be run in reverse to create a refrigerator.