Tapping the top causes compression waves started through the air in the opening (which is why it only works from the top.) The compression waves bounce at the bottom and become expansion waves. The compression and expansion causes agitation which foams up the beer.
Tell me more...
The layman's summary from Scientists discover why beer bottles overflow after a sudden impact:
A lot happens in the short period of time between tap and torrent. The
moment some jerk clocks your bottle's mouth, a compression wave
travels down through the glass. When the wave hits bottom, it's
reflected as an expansion wave that travels through the beer. These
waves keep bouncing back and forth, with the compression waves
breaking up the CO2 bubbles in your beer into thousands of incredibly
tiny microbubbles, and the expansion waves causing those microbubbles
to violently expand into skyrocketing plumes.
The result? Millions and millions of expanding CO2 bubbles turn your
beer into foam shooting out of your bottle. With any luck, it spills
all over your jerk friend's pants.
The abstract from the published article:
A sudden vertical impact on the mouth of a beer bottle generates a
compression wave that propagates through the glass towards the bottom.
When this wave reaches the base of the bottle, it is transmitted to
the liquid as an expansion wave that travels to free surface, where it
bounces back as a compression wave. This train of
expansion-compression waves drives the forced cavitation of existing
air pockets, leading to their violent collapse. A cloud of very small
daughter bubbles are generated upon these collapses, that expand much
faster than their mothers due to their smaller size. These rapidly
growing bubble clusters effectively act as buoyancy sources, what
leads to the formation of bubble-laden plumes whose void fraction
increases quickly by several orders of magnitude, eventually turning
most of the liquid into foam.