When water heats up, it expands and produces steam. When a water heater produces hot water, it causes the pressure to build up within the supply line, stressing the pipes, and eventually causing them to burst or leak. Expansion tanks are designed to keep this steam and expanded water from causing pipes to burst due to built up pressure. They go on the supply line and provide extra space for excess water to flow back into, thus relieving the pressure. This is why you may also hear them called "overflow bottles".
Expansion tanks also prevent water pollution. Water flows into your home through a main supply line, whether it's city or well water. If it is set up correctly, there is a check valve that makes sure any water that flows out of the main supply and into the rest of the home does not "backflow", or flow backwards into the main water supply. Backflow could contaminate the water supply and cause those using it to get sick. These contaminants, like minerals from corroding pipes or bacteria, can pollute the water, and while you may have grown accustomed to the unique substances in your water, others have not. It's especially important in e-coli cases, where backflow could cause whole neighborhoods to be affected. Making sure you have a working check valve is super important, and expansion tanks will keep the pressure off these valves and prevent them from breaking easily.
Expansion tanks typically last between 5 and 8 years. If taken care of well, they can last much longer. We typically recommend replacing them with your water heater, since a brand new water heater is likely to produce more pressure than one that worn out. Expansion tanks can get wore out, just like everything else that get's used consistently. Thus, to keep it from bursting under these new amounts of pressure, it's best to replace it with the water heater and not chance a huge mess.