The thermodynamic efficiency is an ideal quantity. It assumes that the heat engine is fully reversible (which is impossible)and that there is no friction (which is also impossible). The engine operates at a hot temperature, T_hot, which is the temperature inside the cylinder at the initial stage of the expansion. Since that stage is idealized as an isothermal step, the temperature does not change during that step. Then all cooling occurs in the adiabatic expansion step. The compression occurs at a constant temperature known as T_cold. As shown in the following, T_hot and T_cold uniquely define the thermodynamic efficiency. Obviously, this ideal definition is the limiting case of the best possible efficiency. Inclusing of irreversibility, friction and any deviation of the steps from their ideal form will lower the efficiency.
How does this relate to a real engine? In a real cylinder (shown below) we can see that the system is not a closed system. The valves permit injection of the fuel and oxygen prior to ignition and heating, but also permit the release of the gas on the compression step. The Carnot cycle assumes that the cylinder is closed. Irreversibility is inherent in any real expansion since it is not infinitely slow and it is essentially impossible to gradually change the opposing force to maintain equilibirum throughout the expansion. The engine design attempts to do this by having a crankshaft that changes the length of the lever arm that the piston pushes a gradually as possible. While it is not perfect, by any means, the design does mimic the gradual change that one sees in an isothermal expansion.