The problem you are describing is often called water hammer. It can happen on a normal faucet, but is more common on pipes attached to a washing machine or dishwasher. In both of these machines, the valve is electrically operated -- it's called a solenoid valve (see How Electromagnets work for a discussion on solenoids). Solenoid valves close very quickly.

Imagine the following scene. The washing machine is filling, so the valve is open. Water is rushing through the pipe toward the washing machine. When the washing machine is full, the valve slams shut very abruptly. In the pipe, there may be 5 or 10 pounds of water that had been moving at a significant speed, and now it suddenly comes to a complete halt. The water's momentum carries the entire pipe system forward, and it's the shifting of the pipes that makes the noise. If it is a long, straight pipe carrying the water, the amount of motion, and therefore the noise, can be especially significant.

Obviously this is not good for the pipes -- each time you hear the noise, that means your pipes have been stressed and flexed. The normal solution is to put a "shock absorber" in the pipe near the valve. A simple shock absorber could be a foot-long piece of vertical pipe attached near the valve. This pipe is filled with air. Now, when the valve closes, the moving water rushes up into the vertical pipe and compresses the air, so it slows down gradually. The problem with a simple vertical pipe is that the bubble can get dissolved into the water, so a sealed system using a rubber bladder can be purchased (this solution also allows other orientations besides vertical). Another approach is to use a spiral of copper pipe.

Here are some interesting links: