Alexander Isak got the hosts off to a dream start, keeping his cool from the penalty spot to net his 13th league goal of the season.
West Ham managed to turn the game in their favour before the break thanks to goals from Michail Antonio and Mohammed Kudus.
Jarrod Bowen then added a third within three minutes of the restart to leave the Magpies with a mountain to climb.
As it proved, Newcastle were up to the uphill task, restoring parity through another Isak penalty before Barnes found the net on two occasions to spark wild scenes of jubilation among the home support.
Although Anthony Gordon was sent off in stoppage time, it did little to dampen the mood at St James' Park as Eddie Howe's side saw out the remainder of the contest to claim a second straight home victory.
Newcastle could not have wished for a better start in the lunchtime kickoff taking the lead from the penalty spot inside the opening six minutes.
Vladimir Coufal's clumsy challenge on Gordon allowed Isak to send Alphonse Areola the wrong way and fire his spot-kick in off the inside of the post.
Unfortunately for the home faithful, they were handed a setback when Jamaal Lascelles was withdrawn in the 17th minute due to an apparent knee injury.
Just four minutes later, Antonio latched onto Lucas Paqueta's inch-perfect ball over the top to calmly slot home his fourth league goal of the season.
After being pegged back by David Moyes's troops, Newcastle went close to restoring their slender advantage on a couple of occasions.
Gordon headed wide from Jacob Murphy's inviting cross, before Bruno Guimaraes curled a long-range strike against the crossbar.
The two sides looked destined to head into the break on level terms until West Ham unlocked the Newcastle defence with a quick free-kick in the 10th minute of first-half stoppage time.
After collecting the ball off Paqueta, Bowen attempted to cut inside onto his favoured left-foot, with his heavy touch acting as a layoff for Kudus to bend the ball into the far corner.
Kudus then repaid Bowen in the opening stages of the second period, leading a breakaway before feeding the England international, who raced into the box and sent his right-foot shot past half-time substitute Martin Dubravka.
Newcastle experienced more frustration when Tino Livramento was forced off before his replacement Miguel Almiron picked up his own injury issue.
Despite having to cope with those injury setbacks, the Magpies managed to stage a late comeback to score three times in the final 15 minutes.
First, Isak calmly dispatched his second penalty of the contest after Manchester City loanee Kalvin Phillips had fouled Gordon in the box.
The Sweden international then turned to provider, sending Barnes racing through on goal to calmly slide the ball through the legs of Fabianski.
Not content with netting the equaliser, the former Leicester City man produced an unstoppable strike in the 90th minute to snatch all three points.
Barnes collected the ball on the outside of the box, before shifting it onto his favoured right foot and curling a sublime effort into the far corner.
Newcastle were given one more hurdle to overcome after Gordon was shown a second yellow card for kicking the ball away, but they ultimately held firm to claim maximum points and move up to eighth spot.
The Magpies are now just one point adrift of the seventh-placed Hammers, who are now winless in three Premier League matches.