A loud banging from your plumbing pipes is called a water hammer. It often occurs when you shut off a tap, although you may also hear it while a tap is running or after a toilet flush. The sound isn't just alarming because it's loud, it's also alarming because it indicates a problem with your plumbing system.
The bang is the result of the moving, churning water in your pipes coming up against a barrier. Water within your plumbing system doesn't flow smoothly. Instead, it moves in a turbulent and high-force flow through the pipes. If that flow is too forceful, then it can literally bang against anything in its way and cause a hammering noise.
Most often, the barrier the water hits is the closing valve on the line when the tap is turned off. You may also hear a water hammer in older homes when the water is flowing. This is due to the severe 90-degree pipe angles that were more common in the past. The water doesn't so much as flow around the corner as it slams into the adjoining pipe. Excessive water pressure can also lead to hammering.
Although the noise can be annoying, it's not necessarily a problem. The problem comes from the vibration that occurs when the water slams into the obstruction. This vibration leads to pipe movement that can develop into leaking joints as the pipes literally shake apart. The pipe themselves may eventually split or burst from the movement.
The cause of the hammering may also be a problem. For example, if the pressure is too high due to a failed pressure regulator valve, then that valve needs to be replaced before the heightened water pressure damages, not just the pipes, but any fixtures or appliances the water flows into.
Repair needs depend on the root cause of the water hammer. If a valve is closing too quickly along the line, then your plumber may simply need to replace or readjust the problem valve. If it's the pressure regulator that has failed, then a new one can be installed.
For older homes where 90-degree pipe angles are the cause, the best solution is to upgrade the water pipes in your home. Flexible piping can be put in that doesn't require any sharp angles. This allows the water to flow more smoothly without slamming loudly into the pipe walls.
Contact a plumbing service in your area for more assistance.