The How Stuff Works website attempts to explain it:
When you shake a soda can, some of the gas comes out of solution. There are a bunch of carbon dioxide bubbles clinging to the inside of the can, trying to get out. When you open this can and the gas rises to escape, it doesn’t only push on the liquid at the very top of the can. Because gas bubbles are stuck to the top, sides and bottom of the can, they force all of the liquid out because all of the liquid is blocking their upward path.
When you tap on the can, you knock bubbles off the bottom and sides of the can, at which point they rise to the top (because gas is lighter than liquid) and there is only a small amount of liquid blocking their escape when you open the can.