From isolation to a heat bath
Real experiments almost never fix a system's energy. Far more often we fix its temperature by keeping it in contact with a large reservoir — the lab bench, the atmosphere, a water bath. Energy then flows freely back and forth, and the system's energy fluctuates around an average. The ensemble for this situation, the most useful one in all of physics, is the canonical ensemble: fixed T, V, N.
Here is the elegant derivation. Consider the system plus a huge heat bath, together isolated with total energy E_{\text{tot}}. When the system sits in one particular microstate of energy E_i, the bath must hold E_{\text{tot}}-E_i, and the number of ways the bath can do that is \Omega_{\text{bath}}(E_{\text{tot}}-E_i). By the fundamental postulate the probability of the system's microstate is proportional to that bath multiplicity.
Deriving the Boltzmann factor
Work with the log. Since E_i \ll E_{\text{tot}}, expand the bath's entropy S_{\text{bath}}=k_B\ln\Omega_{\text{bath}} to first order around E_{\text{tot}}: S_{\text{bath}}(E_{\text{tot}}-E_i)\approx S_{\text{bath}}(E_{\text{tot}}) - E_i\,(\partial S_{\text{bath}}/\partial E). But that derivative is exactly $1/T$. So \Omega_{\text{bath}}\propto e^{-E_i/k_BT}, and the probability of the system being in microstate i is the celebrated Boltzmann factor:
The probability of a microstate in the canonical ensemble. States of higher energy are exponentially less likely; the 'inverse temperature' \beta=1/k_BT sets the steepness. Z is the normalizing constant.
The partition function Z
Normalization (\sum_i P_i = 1) forces the constant Z to be a sum over all microstates of their Boltzmann factors. This object — the partition function, Z for the German Zustandssumme, 'sum over states' — is the single most important quantity in statistical mechanics.
The partition function: a sum over microstates, or equivalently over energy levels weighted by their degeneracy g_n. Once you can compute Z, you can compute everything.
Extracting all of thermodynamics
Why is Z so powerful? Because thermodynamic quantities are its derivatives. The average energy is a single derivative of \ln Z:
The mean energy from the partition function. This turns a hard average over 10^{23} particles into a routine derivative of one function.
The true jewel is the bridge to free energy. Just as S=k_B\ln\Omega bridges the microcanonical ensemble, the canonical ensemble's bridge is to the Helmholtz free energy F=E-TS:
The master formula. Compute Z, take F=-k_BT\ln Z, then differentiate to get entropy, pressure and chemical potential — the entire thermodynamics of the system.
Fluctuations and heat capacity
The canonical energy is not sharp — it fluctuates. A second derivative of \ln Z gives the size of those fluctuations, and remarkably it is tied to a measurable response, the heat capacity:
Energy fluctuations equal k_BT^2 times the heat capacity — a fluctuation–dissipation relation. The relative spread scales as 1/\sqrt{N}, so for macroscopic N the energy is effectively sharp.
This is profound and practical at once. It says the spontaneous jiggling of a system's energy (fluctuation) is controlled by how much energy it absorbs when heated (response). And because C_V\sim N while \langle E\rangle\sim N, the relative fluctuation \Delta E/\langle E\rangle\sim 1/\sqrt{N} vanishes for macroscopic systems — which is why the fixed-energy microcanonical and fixed-temperature canonical ensembles give identical thermodynamics. We will make that equivalence explicit in the final guide.