Water expands as it heats — around 2% by volume from cold to a normal storage temperature. In a 50-gallon tank that is roughly a gallon that has to go somewhere.

Open and closed systems
An open system lets the expanded water push back into the municipal supply main. This is how houses worked for a long time.
A closed system has a one-way device between the house and the main — a backflow preventer, a pressure reducing valve with a check, or a check valve — so the expansion has nowhere to go.
Many utilities now require backflow prevention, which means a great many houses have become closed systems, frequently without the homeowner knowing.
What happens without an expansion tank
Pressure rises sharply each time the heater fires, since water is nearly incompressible.
The temperature and pressure relief valve opens, discharging a small amount of water, then reseats. That is the valve working as designed.
Repeated over years, the valve's seat erodes and it begins to weep continuously — at which point it is replaced and the new one does the same thing, because the cause was never addressed.
Meanwhile the pressure cycling stresses the tank, the fittings and every appliance connected to the plumbing.
The expansion tank
A small pressurised vessel with a diaphragm and an air charge on one side.
When water expands, it compresses the air cushion instead of raising the system pressure. When the water cools and contracts, the cushion pushes it back.
Fitted on the cold supply near the water heater, it is inexpensive and it is required by code on closed systems.
Setting it up
The air pre-charge must match the house's static water pressure. A tank charged at the factory to a standard figure and installed on a system at a different pressure does not work properly.
Measure the incoming pressure with a gauge on a hose bib, then set the tank's air charge to match before it is connected to water.
Check it periodically. Tanks lose their charge over years, and a waterlogged expansion tank does nothing at all. The test: tap it — the top should sound hollow and the bottom solid. Or press the valve stem; water coming out means the diaphragm has failed and the tank needs replacing.
The T&P valve itself
The temperature and pressure relief valve is the last line of protection against a tank rupturing.
It must never be plugged, capped, or fitted with a valve.
Its discharge pipe runs to a safe location — commonly to within six inches of the floor, or outdoors — in a size no smaller than the outlet, with no threads on the end and no upward sections that could trap water.
A dripping T&P valve has a cause: excessive pressure, excessive temperature, or a failed valve. Each is investigated rather than the valve being capped, which is a thing people do and which turns a water heater into a pressure vessel with no relief.
Water pressure generally
If static pressure exceeds roughly 80 psi, a pressure reducing valve is required — and note that fitting one creates a closed system, which is when the expansion tank becomes necessary.
High pressure shortens the life of every fitting, appliance and hose in the house. A gauge on a hose bib costs very little and it is worth knowing the number.
The drain pan
A tank in a location where a leak would do damage — an attic, an upper floor, a finished space — needs a drain pan piped to a safe discharge point.
Where no gravity drain is available, a pan with a leak sensor and an automatic shutoff valve is the alternative, and it is inexpensive relative to a ceiling.
Water heaters fail by leaking, eventually, and where the tank sits determines whether that is a mop or an insurance claim.
Seismic strapping
In seismic regions, tanks are required to be strapped to the structure at two points — upper and lower thirds — with the straps and anchors specified.
An unstrapped tank that topples breaks its gas line or its electrical connection, which is the reason the requirement exists rather than the water damage.