Ice melts on a warm day even though melting takes in heat. So giving out heat isn't the only thing that decides whether a change happens. The other is entropy, S: roughly, how many ways the energy and particles can be arranged. Gases have far more entropy than liquids, and liquids more than solids; mixing raises entropy, and so does breaking one molecule into several.
The second law of thermodynamics says the total entropy of the universe (the system plus its surroundings) goes up in any change that happens by itself. Heat given out to the surroundings raises their entropy, which is why exothermic reactions are often favorable, but a big enough entropy gain inside the system can win instead.
The two effects are combined in the Gibbs energy change: ΔG = ΔH − TΔS, with T in kelvin. If ΔG is negative the change is spontaneous, meaning it can go by itself (not that it's fast). If ΔG is positive it won't go unless you drive it, and the reverse is the spontaneous one. ΔG = 0 means the system is at equilibrium.
Watch the units: ΔH is usually in kJ and ΔS in J/K, so divide ΔS by 1000 first. Because T multiplies ΔS, temperature can flip the answer. Limestone breaking down, CaCO₃ → CaO + CO₂, has ΔH = +178 kJ and ΔS = +0.161 kJ/K. At 298 K, ΔG = 178 − 298 × 0.161 = +130 kJ: it doesn't happen. It turns spontaneous above T = ΔH ÷ ΔS ≈ 1100 K, which is why lime kilns run at around 900 °C.
The signs tell the story. ΔH negative and ΔS positive: spontaneous at any temperature (burning). ΔH positive and ΔS negative: never. When both have the same sign, temperature decides: high T favors the side with more entropy.
Practice
Which change has the biggest increase in entropy?
More info
Quick reference
Entropy S
Number of ways to arrange: gas > liquid > solid.
Second law
Total entropy rises in a spontaneous change.
ΔG
ΔH − TΔS (T in K; match kJ and J)
ΔG < 0
Spontaneous (not necessarily fast).
Switch-over T
ΔH ÷ ΔS
The other chemistry and chemical engineering courses