I've seen/listened to people playing the piano where someone will start singing a song or name a song or whatever and after a little while they'll pick it up and start playing the correct chords to the song, even if they don't already know them (like, they know how the song goes and what it sounds like, but not the chords).

I've tried playing along to some songs on YouTube, and some of them I pick up after a few tries while others I can't seem to figure out even though they sound like they should be simple.

So the question is, how do I figure out the correct chord progression for a song as I'm playing it, assuming I know what the song's supposed to sound like?