Esteban Cambiasso had fired in a brilliant half-volley with just 12 minutes on the clock but David Nugent missed the chance to double the advantage, his penalty saved by Adrian, and Cheikhou Kouyate instead equalised for the Hammers after 32 minutes.
Kouyate went close to scoring his second after the break but struck the post and West Ham were made to pay when substitute King pounced with just four minutes remaining.
The win was Leicester’s first in the Premier League for almost three months and means they are now just four points from safety with eight games remaining, five of which are at the King Power.
The home fans got behind their team in a frantic opening as Leonardo Ulloa fired over the bar and Riyad Mahrez had a cross cut out.
Leicester’s positive start paid off after 12 minutes as Nugent’s prod forward bounced off the back of James Collins and sat up nicely for Cambiasso to thunder home from 25 yards.
It looked as though the hosts would double their advantage shortly afterwards as Carl Jenkinson conceded a penalty for bringing down Ulloa in the box.
But Nugent’s poor spot-kick was saved and the momentum then seemed to turn in West Ham’s favour.
Kouyate forced a good save from Kasper Schmeichel before Kevin Nolan headed wide from Stewart Downing’s cross.
An equaliser looked to be coming and it eventually arrived when Alex Song chipped a ball into the box and Kouyate fired into the corner.
The hosts struggled to muster much of a response before the interval but started the second half positively.
Robert Huth glanced a header wide from a Cambiasso free-kick and Marc Albrighton – on at the break for Mahrez – was just unable to connect on the stretch with a Jamie Vardy cross.
Vardy then saw a couple of decent chances go wide, the first a header and the second a shot from a decent position.
Leicester manager Nigel Pearson freshened his attack again by introducing Andrej Kramaric for Ulloa, but West Ham were starting to impose themselves once more as the game opened up.
Downing dragged an effort wide and Kouyate hit the woodwork before bringing a save out of Schmeichel with a curling shot.
However, Leicester fought back admirably and after Kramaric had a shot cleared off the line, he sent the ball towards Vardy, whose shot was diverted in from close range by King for the winning goal.
Leicester: Schmeichel (6), Huth (6), Morgan (6), Schlupp (5), De Laet (6), Cambiasso (7), James (5), Mahrez (6), Nugent (5), Vardy (7), Ulloa (6).
Subs: King (7), Albrighton (5), Kramaric (6).
West Ham: Adrian (7), Collins (6), Cresswell (5), Jenkinson (5), Reid (5), Downing (6), Kouyate (7), Noble (6), Nolan (5), Song (6), Sakho (6).
Subs: Amalfitano (5), Nene (5), Cole (5).
Man of the match: Jamie Vardy.