The Italian defender met a free kick from Dimitri Payet and sent his effort back across keeper Simon Mignolet and into the far corner of the net to seal the London team's first-ever FA Cup win over Liverpool in eight meetings.
The hosts took the lead in the fourth-round replay when Michail Antonio guided a superb volley into the top corner moments before halftime after Liverpool had controlled the early stages without being able to find a breakthrough.
The lead was swiftly wiped out, however, as Philippe Coutinho sidefooted a clever free kick below the jumping West Ham wall three minutes into the second half.
It took 44 minutes for triple winners West Ham, who face second tier Blackburn Rovers in the next round, to break the deadlock.
The fans inside Upton Park were left wondering how the tie stayed goalless for so long with the woodwork being struck three times in the first 37 minutes.
Liverpool, who had manager Juergen Klopp back in the dugout after he missed the draw with Sunderland on Saturday to have his appendix removed, played a young side but they were more than a match for their more experienced opponents.
Christian Benteke forced West Ham keeper Darren Randolph into a good save from a corner before a deflected effort from home defender Joey O'Brien cannoned back off the foot of the post.
Coutinho, on his return from injury, then sidefooted an effort against the post before West Ham got a foothold in the game and Payet became the third player to hit the woodwork from a free kick.
The deadlock was eventually broken when Antonio guided a superbly-controlled sidefooted volley into the top corner.
The lead did not last long as Coutinho drew Liverpool level, waiting for every man in the West Ham wall to jump before rolling the ball nonchalantly beneath them into the bottom corner.
With neither side able to force a winner, the match went into extra time and Benteke was guilty of pulling a good chance wide and then failing to beat Randolph in a one-on-one.
With penalties looming, Ogbonna headed in powerfully to send the home crowd wild.
Original source: West Ham defy Liverpool in extra time