The story
Uncertain. Possibly derived from an old joke: A man in a crowded bar needed to defecate but couldn't find a bathroom, so he went upstairs and used a hole in the floor. Returning, he found everyone had gone except the bartender, who was cowering behind the bar. When the man asked what had happened, the bartender replied, 'Where were you when the shit hit the fan?' Other possible sources include American or Canadian military slang or agricultural manure spreaders.
Etymology from Wiktionary, CC BY-SA 4.0.