When the boat heels over, the center of effort of the sails moves to leeward much more than the center of lateral resistance. When the boat is not heeling, these are essentially directly in line so there is no torque about the vertical axis. However, when the boat heels, since the CE is more to leeward, you get a torque about the vertical axis.
There are additional effects from drag and the change of the water flow on the rudder but in a well designed boat, those should be really small.
If you have the chance to sail a small light boat (10-20ft) with the tiller lashed, it will be really instructive. Heeling the boat more will cause you to head up, heeling less will cause you to fall off. Moving weight forward will cause you to head up and moving weight aft will cause you to fall off. Then you can start messing with sail trim.