Why does 'like dissolves like?' To get at the thermodynamic reason behind the rule, Hess's Law of Heat Summation lets us imagine dissolving a solute in a solvent as a step by step process. Let us use our imagination to take the solution process down a pathway that is easy to analyze in terms of internal energy change.
Imagine pulling the solute apart from itself, pulling the solvent apart from itself, then letting the solute and solvent come together. In the first step, enthalpy must increase to separate the solute molecules from each other (we have to increase the electrostatic potential energy of mutually attracting particles, an internal energy increase, requiring heat flow from the surroundings) and likewise, enthalpy must increase to separate the solvent molecules from each other.
The stronger the respective intermolecular attractions within either the solvent or solute, the more internal energy increase will have been necessary to reach this stage in our imaginary pathway. Now, we can picture the system at a stage where all the particles are separated from each other.
Then, as the next step, imagine the system falling back together into the new arrangement, the solution, the final state of the system. So instead of the solute and solvent particles seeking out their old attractions, the solute associates with solvent and vice-versa.
If the quality of intermolecular attraction in both solute and solvent is similar, the net process will not involve a large increase of internal energy. The energy that went in to the system will now come out. With 'like dissolving like', the overall process will be only slightly endothermic (usually). If, on the other hand, one of the two, the solute is polar and the solvent is nonpolar (or vice versa), the imaginary process would call for the input of a large amount of energy and it would not be recoverable. The polar molecules of the solute would have nothing to grip in a nonpolar solvent, no charge densities in the solvent, to form a solution, and the result would be a very endothermic solution process. In an endothermic process, heat must flow in to the system (positive enthalpy change). Inward heat flow is not likely to happen, which is why the equilibrium points the other way toward insolubility.