Steve Bruce’s side had squandered a two-goal lead in the closing stages at St James’ Park, but Willock came off the bench to snatch a priceless winner.
Newcastle are nine points clear of the relegation zone with just six games remaining in a turbulent season on Tyneside.
The Magpies had taken control in the first half thanks to Issa Diop’s own goal and a blunder from West Ham keeper Lukasz Fabianski for Joelinton’s strike.
West Ham defender Craig Dawson was sent off for two bookings in between those goals.
But the Hammers set up a tense finale when Diop got one back before Jesse Lingard equalised with a penalty 10 minutes from full-time.
On-loan Arsenal midfielder Willock settled a pulsating encounter with his thunderous header, putting Newcastle in position to avoid relegation.
Staying up would be an impressive achievement for Bruce, who has battled with dressing room rifts and distracting takeover talk.
The defeat was a big blow for West Ham as they chase an unexpected place in next season’s Champions League.
David Moyes’ side remain fourth, but Chelsea are just one point behind with a game in hand, while fifth-placed Liverpool would go above the Hammers if they beat Leeds on Monday.
Newcastle made a bright start as Jacob Murphy’s 25-yard effort brought Fabianski into action.
Allan Saint-Maximin tested Fabianski after carving through the West Ham defence in typically flamboyant fashion.
Newcastle took the lead in the 36th minute when Dawson, in his attempts to recover from a heavy touch, felled Joelinton on halfway.
He could only look on as Saint-Maximin sped away, twisted his way past the retreating Mark Noble and then saw his weak shot bundled over the line by Diop as referee Kevin Friend allowed play to continue.
Dawson’s misery was compounded when, having already been booked for a 12th-minute foul on the Brazilian, he received a second yellow card.
Joelinton, who had previously scored only two Premier League goals this season, was gifted his third by Fabianski when he spilled Matt Ritchie’s 41st-minute corner, leaving the Brazilian striker with a tap-in that he couldn’t miss.
However, Diop headed the Hammers back into it with 17 minutes remaining.
West Ham draw level when, after a VAR check, Ciaran Clark was penalised for handball and on-loan Manchester United midfielder Lingard struck from the spot for his ninth goal in 10 games.
But there was a sting in the tale as Willock, who had only been on the pitch for seconds, thumped his header past Fabianski from Ritchie’s cross in the 83rd minute.