When you turn off the faucet in your bathroom, the water rushing through the pipes does not magically stop. Like any matter, it has mass and momentum, and when it slams into a closed valve, it generates a lot of pressure that your pipes just have to deal with. This is the everday torture of "water hammer," and explains the basics of how it works, and how engineers prevent it from blowing up your plumbing.
Essentially pressure surge—or a literal wave if there's enough room—water hammer is caused by a sudden change in water's momentum. If water is barreling towards a destination, and a sudden impediment is placed in front of it, the power of it ramming the impediment at full speed can be enough to break the pipes given that fluids are generally non-compressible, an important principle behind hydraulics. The same phenomena occurs in other fluids, where it is called a fluid hammer.
Engineers can mitigate the affects of such hammer by slowing the water down, thus slowing momentum, with devices like flywheels or other mechanisms that introduce pockets of air that can be compressed instead of the surging fluid that refuses to squish. But hammers remain tricky. They are one of the reasons why there's growing international protest against pipelines, and also a good reminder to turn your water off slowly.