This is called water hammer. To disperse this force, shock absorbers are placed in the plumbing system. Many designs of these devices exist. Some are just coils of copper tubing that flex. Others are canisters with a diaphragm.
Water is on one side of the diaphragm and air is on the other.
The diaphragm flexes, compressing the air and absorbing the force. The most common device, however, is simply a capped piece of pipe rising vertically about 2 feet from the horizontal water pipe. The trapped air in the pipe compresses momentarily to absorb energy. Usually, one of these devices is at the end of each pipe run. There could be many in a house, and they could be in the attic or in a wall.