The Hammers needed three points at the King Power Stadium or Southampton would have been promoted to the Premier League on Monday night.
Leicester had dented the Hammers' hopes after Jermaine Beckford gave the Foxes the lead on 34 minutes, with Ben Marshall's cross headed into the net by the striker at the back post.
West Ham boss Sam Allardyce was given a huge boost five minutes before the break though as Mark Noble's delivery was turned home by Winston Reid to ensure the score was level at the break.
West Ham stormed out of the blocks in the second half and they turned the game on its head when Jack Collison scored a stunner after 59 minutes to send the visting fans into raptures.
Kevin Nolan could have made it 3-1 in stoppage time but his shot was charged down by Kasper Schmeichel.
West Ham move on to 83 points and face Hull at Upton Park on Saturday while Southampton have 85 points and host Coventry in the race to finish second in the Championship.
Former Toon boss Sam Allardyce said after the win: "We got exactly what we wanted tonight.
"It's a good night for us and of course we've stayed in the race until the very end, which is what we wanted and we have to do our job on Saturday and just hope that Coventry can do a little bit at Southampton and then we'll wait and see.
"It's one game left and who knows, if Southampton get the jitters or the nerves, or Coventry play with no fear because they've been relegated now, who knows, but we need to make sure we do our job and win against Hull.
"Whatever happens after that it's been a great season for us. Whether we go straight up, which is a slim chance, or whether we go in the play-offs, it's still been a very good season from the players and everybody concerned."
West Ham captain Kevin Nolan: "We knew we had to put the pressure on Southampton. We're not expecting much on Saturday, but it was always positive for us to make sure that we kept our momentum going and our wins going.
"Hopefully Coventry can do us a favour on Saturday, but it will be a bit tougher for Southampton now.
"We didn't want to give it to them, we wanted to make them earn it. They've been great all season but let's see if they can get over the finishing line now.
"We've been like that away from home all season, we're just disappointed with our home form, that's what's let us down.
"Winning the game was the most important so we could keep the winning momentum going.
"It's a fantastic achievement by this lot, we've got a young squad here and they are only going to get better and we're looking forward to Saturday.
"The pressure is on them (Southampton), they've got to win.
"Coventry have got nothing to play for so maybe they will be on their holidays, but hopefully they will go and put in a good performance and be professional about it and they can do us a favour and we can do our business.
"But it's down to us again, we've just got to make sure we win on Saturday and then when we come off see if it's enough."