I understand the second law of thermodynamics in terms of the improbability of the high--and-low-velocity particles of a gas to separate themselves so that at one moment they are at different sides of a container. But I can't yet understand how this relates to the efficiency of heat-to-work conversion.
I have in mind the following scenario: a single container with two volumes, separated by a movable wall.
You heat one side and the pressure increases because the molecules gain kinetic energy and move faster. They bump into the movable wall and transfer their own kinetic energy to it. The movable wall moves towards the cooler volume, until the pressures are equal. You did work (you moved the wall). Assume no friction.
Where does the second law come in? What does it mean to say that the entire extra kinetic energy of the particles in the heated volume can't be converted into work, in terms of molecules and transfer of kinetic energy?