With the engine running, chances are it's not the battery connections themselves as the altenrator can provide power sufficient to keep the engine running in most cases even without a battery (not that it's recommended to operate that way, for several reasons). I actually have some personal experience with that; I had a car decades ago where one of the battery lead clamps was worn/loose and would occasionally vibrate out of contact. The headlights would dim and the wipers slow as one came to a stop and the engine idled, and then get back to normal-ish when moving along again. When one reached the destination and turned the key off, only then would everything go completely and eerily quiet and dead.
Ignition switch or wiring harness, dashboard ground, that sort of thing would be my guess, as others have suggested.