How does the second law of thermodynamics affect the efficiency heat-to-work conversion, at the molecular level?

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?

• To achieve the Carnot limit, every process must occur reversibly. This means that when heat is transferred from the working substance to the heat sink they must have the same temperature. So if the heat sink is at absolute zero, so is the working substance. 0K is the state of minimal entropy, so everything is nicely ordered and no energy is going anywhere unexpected. Additionally at $T=0$ the working substance is in its ground state, so there is no energy to extract. Commented Apr 18, 2015 at 14:41