Issa Diop's own goal gave Newcastle the lead at St. James' Park, with Hammers defender Craig Dawson then receiving a red card after fouling Joelinton in the build-up to the opener, before the Brazilian forward doubled the hosts' lead with a tap-in from close-range after capitalising on Lukasz Fabianski's goalkeeping error.
Despite their commanding position, Newcastle found themselves back on level terms through Diop's second-half header and Jesse Lingard's penalty, after VAR spotted a handball from Ciaran Clark, before Joe Willock's late header secured maximum points for the Magpies, who move nine points clear of the relegation zone.
Steve Bruce's side headed into the Premier League clash against the in-form Hammers, who sat in fourth place, aiming for successive league victories for the first time since December following a 2-1 win at Burnley.
The hosts, who beat West Ham 2-0 at the London Stadium on the opening day of the season, got off to a bright start in the opening stages as Sean Longstaff saw his shot from the edge of the penalty area deflect wide before the midfielder's resulting corner floated over Fabiański, with Angelo Ogbonna importantly heading the ball away to safety.
Jacob Murphy, who netted United's equaliser in last weekend's victory against Burnley, went close to netting in successive games as the right wing-back's fierce low drive was held by the diving Fabiański.
Martin Dúbravka was on hand to deny Dawson from breaking the deadlock as the Slovakian shot-stopper crucially held the defender's header following Mark Noble's teasing free-kick into the penalty area.
In the 36th minute, United opened the scoring in fortunate circumstances as Saint-Maximin picked up the loose ball, after Dawson's late challenge on Joelinton following a heavy touch, and drove past Noble before the winger's strike was inadvertently bundled into the net by Diop.
After picking up a yellow card prior to his foul on Joelinton, Dawson then received his marching orders as the Hammers were reduced to ten men.
Five minutes later, Newcastle doubled their advantage as Joelinton bagged his second goal in consecutive home games after netting United's opener against Tottenham Hotspur.
As Matt Ritchie's floating corner was spilled by Fabiański, the 24-year-old Brazilian was alert inside the six-yard area to turn the ball into the net.
In the second-half, Dúbravka was forced into an early save, denying Jarrod Bowen after the forward, who had netted in his last three consecutive games, had advanced into a dangerous position inside the box.
The visitors continued their push to halve the deficit as Ben Johnson's cross from the left flank flew towards Vladimír Coufal at the back post but the right-back's header was kept out by Dúbravka.
Newcastle were dealt a disappointing blow in the 73rd minute as Diop netted for West Ham, with the French centre-back atoning for his earlier mistake after beating the offside trap and heading home from Bowen's inviting cross from the right flank.
The Magpies found themselves on level terms seven minutes later as Lingard netted his ninth goal in ten Premier League outings from the penalty spot.
After a VAR check, referee Kevin Friend awarded a spot-kick for the Irons as Coufal's long throw-in into the box struck the arm of Clark after the Irish centre-back attempted to head the ball away to safety.
Following a lengthy wait, Lingard confidently dispatched his attempt from 12 yards, firing the ball home via the left-hand post despite Dúbravka guessing the correct way.
More drama unfolded as Murphy saw an effort cleared off the line by Johnson after arriving onto Callum Wilson's pass inside the penalty area after the striker's intentional pass to Miguel Almirón fell kindly into the defender's path.
In the 83rd minute, Newcastle retook the lead as Willock marked his introduction from the substitutes' bench in immediate fashion with United's third goal of the afternoon.
After Ritchie's inviting cross flew into the penalty area, the 21-year-old midfielder, who came off the bench to net United's equaliser against Spurs, rose above Johnson to direct a fierce header past Fabiański via the underside of the crossbar.
With eight additional minutes of stoppage-time, a nervy finale was set but Newcastle held firm to prevail in a five-goal thriller.
Newcastle United: Martin Dúbravka, Jacob Murphy, Federico Fernández, Ciaran Clark, Paul Dummett, Matt Ritchie, Sean Longstaff (Joe Willock 80), Jonjo Shelvey, Miguel Almirón, Allan Saint-Maximin (Callum Wilson 64), Joelinton (Andy Carroll 90+8).
Subs not used: Mark Gillespie, Emil Krafth, Javier Manquillo, Jamal Lewis, Jeff Hendrick, Dwight Gayle.