The Partition Function is the central tool for deriving the statistical mechanics for a given physical system. It is denoted Z. Actually, this Z isn't nearly impressive enough. Usually the partition function Z is the biggest Z on the page, has two diagonal bars, is rendered with curvy bars, or something to set it off as a BIG DEAL. Often, all of the above are employed simultaneously, especially when the Z is built on the Grand Canonical Ensemble. The folks who devised statistical mechanics were nothing if not self-important.
What makes it so special? It incorporates all of the information relevant to statistical mechanics about all of the possible states of any system that's anywhere near equilibrium. Once you have it, you can take a few well-chosen derivatives to get a wide range of important thermodynamic properties. Not quite all of them, but many. Even without taking derivatives, just constructing it gives you a picture of the kinds of states the system is liable to occupy.
How to build the Partition Function
First you think of all of the possible states of the system. This is often not as hard as it sounds, especially if you're willing to make some approximations. Like, the ideal gas in a bottle. Each particle can be at any location in the volume of the bottle, and while there it can be moving any speed and direction, and rotating about any axis
at any speed. Sure, in real life they won't overlap, which cuts down on the possibilities, but as long as the gas is not highly pressurized that's a pretty minor inaccuracy. So far, this makes sense. If it's going to talk about all the states, you need to know what they are.
Second, for each state, multiply it by e-E/kT, with E the energy of the state, k Boltzmann's constant, and T the temperature. Now, this one is a bit more obscure. Where did that come from? Well, the most mysterious element of that expression from a physics point of view is "temperature". What does 'temperature' even mean? We all know what it means in terms of comfort, cooking, and thermometers; but in terms of physics up to this point, it hadn't been defined at all. Really. The notion that the temperature is proportional to the energy of the system follows from this and some additional assumptions that often apply. Just 'often', not 'always'.
When it comes down to it, this appearance in the partition function effectively defines temperature: kT is the energy scale of the decay of terms in the partition function. As an explanation for why the term takes that form, that sucks, I grant. Justifying it is possible, but way beyond the scope of this writeup. But as a definition of temperature, it does fine. It even has some... interesting consequences for some unusual systems described below.
So, last step. To get Z, you just add up all of these terms. Big deal, you may say. But here's how you think about it. The partition function is sort of like a deck of cards, with one distinct card for each of the specific states, and each of those cards repeated a number of times proportional to that exponential factor.
The probability that the system is in a given state is the same as the probability that you draw one of that state's cards.
If the temperature is low, then states with high energy get very few cards, while states with little energy get a lot of cards. If the temperature is high, states with low energy still get more cards, but not as many more, so the average energy of the system increases.
Canonical vs Grand Canonical Ensembles
What I described was called a Canonical Ensemble. In this case, the bottle could exchange energy with its environment. Let's open that bottle. Now air can exchange freely with the environment! We don't know how many molecules there are in our bottle. This has a surprisingly small change to the recipe I gave above. You still do the same things - identify all of the possible states, and calculate their energy, and so forth - but you do it for any number of particles. Big difference in execution, but the same idea in principle... mostly. There is a special part of that energy we need to consider, called the chemical potential. This is the part of the energy that is the energy cost to add a molecule to the system. Why is it called the chemical potential?
Let's leave air behind a bit and look instead at a more… chemical system. A closed container of water, say, with a mix of H2O, OH-, and H30+. Now, there's an energy associated with converting 2 H2O molecules into a pair of OH- and H30+. If you calculate the partition function on this system, the energy to split that H2O will be the chemical potential. All other uses are by analogy to the chemical uses such as this one.
Any partition function calculated with a variable number of constituents and an associated chemical potential is called a Grand Canonical Ensemble, and there are even more thermodynamic variables it lets you calculate.
If you do this with a quantum mechanical system, you'll need to take into account whether each sort of constituent is a fermion or a boson. That'll change around the counting step fairly noticeably - you only count how many are in each single-particle state rather than assigning each one an individual single-particle state, and if the particles are fermions, you only let that count be 0 or 1. That has some interesting consequences, but the recipe given above still holds.
The finite case
Let's think instead about a very simple example. There are two states, one with energy 0, and one with energy E (parallel cases can be built with other finite ranges of energy, but let's stick to 2 states). The partition function is just 1 + e-E/kT. The probability of the low-energy state is 1/Z, and the probability of the high energy state is e-E/kT/Z. From this, you might think that there's no way for the high-energy state to get more populated than the low energy state.
Well, that's not just wrong, it's obviously wrong. You can prepare one system that way and let it sit in isolation, and prepare a bunch of others mostly that way, and only let them exchange energy with each other, and they'll be at some temperature. Conservation of energy isn't letting them get down to that low-energy state. So what's the temperature?
It's negative. In this case, the temperature indicates the energy scale of the decay as the energy decreases from the maximum energy, rather than the other way around. Obviously this can't apply if, as in the usual case, there's no energy maximum. It's easier to understand this behavior if you consider things in terms of 1/kT, which is given the symbol Β (Beta). A high Β is a low temperature - the lower-energy state is preferred strongly. As Β drops, the temperature rises and the two probabilities approach. Zero Β is the case where the two states are equally probable, and negative Β are cases where the higher energy state is more probable.
This last class of cases is termed population inversion, and is vital to the functioning of lasers. So negative temperatures are even physically real!
I'll leave you to chew on that.