Diagnosing appliance that shocked me
Thanks to everyone and particularly to ThreePhaseEel for his suggestion that helped me diagnose the issue.
My non-contact voltage detector picks up current within about 18 inches of the fan housing, even while the fan is turned off. The other two bathrooms have the same model fan assembly installed, but the voltage detector doesn't read anything from just beneath their housings, even when they are turned on!
Clearly something is not wired correctly in the bathroom where I got shocked, and the shock which I received when I touched the air compressor must have originated from the fan assembly/housing, which is apparently hot all the time.
I didn't get shocked when I removed the cover from the fan assembly, or when I touched the inside of the housing while inspecting it, but maybe that's because I was standing on a fiberglass ladder. My guess is that I got a shock when my body formed a circuit between the electrified housing and the air compressor (which was plugged into the wall and thus able to continue the circuit), and I just happened to be feeling the shocks on the air compressor side even though the current was coming from the fan housing.
Update:
The cables on my multimeter are not long enough to reach from the fan housing to a safe grounding source, but my uncle brought over a wired voltage tester with long cables. We put one probe into the ground port of the bathroom outlet, and touched the other probe to the fan housing. The tester immediately beeped, verifying that the housing is hot.
Update 2: Final diagnosis of the disastrous wiring

The fan (which is a 1500W combo unit with a heater and dome light) was on the same 15-amp circuit as all of the ceiling lights in that half of the house. I don't know how it never tripped the circuit breaker.
The vanity lights were on their own 15-amp circuit. Obviously whoever originally did the wiring mixed up the two, as the fan should have been on its own circuit.
The fan housing has a grounding wire, but the grounding wire was not connected on either end.
The fan housing was hot because the wiring to the vanity lights was frayed at the junction box, which is attached to the same metal framework as the fan housing.