Michael Owen netted a hat-trick and Alan Shearer moved to within one goal of Jackie Milburn's Newcastle scoring record as the Magpies took the points.
Owen's neat finish put Newcastle ahead on five minutes, but Nolberto Solano inadvertently put Marlon Harewood's cross into his own net to equalise.
A header from Owen restored the lead, then Shearer's low drive made it 3-1.
Harewood's penalty after Shola Ameobi's handball set up a tense finale, but Owen's late finish sealed the win.
The scoreline was tough on West Ham, who were bright throughout the match, but Owen's class proved the difference between the sides.
The Hammers fashioned the first chance in the opening minutes when Carl Fletcher tested Shay Given with a long-range effort.
But they were stunned when Owen gave the Magpies the lead in typical style after five minutes.
The England international did well to control Shearer's through pass and then sidefooted home despite the best efforts of Roy Carroll.
The Hammers still looked bright despite the setback and their 20th-minute equaliser was deserved - even if the manner was somewhat fortuitous.
Harewood sent in a superb low cross and Titus Bramble's attempted clearance hit the unfortunate Solano and flew into the net, giving Given no chance to save.
West Ham continued to look threatening yet it was Owen who had the final say in the first half.
The striker connected with Solano's cross to divert the ball past Carroll - even though the final touch appeared to come off his shoulder.
There was no drop in intensity from the home side after the break and they came desperately close to an equaliser three times in quick succession.
First Harewood was denied by the outstretched foot of Given before Hayden Mullins' 25-yard sidefooted effort rebounded off a post.
Then Shearer, of all people, proved his value in defence as he cleared off the line from James Collins' goalbound shot.
The veteran striker was back in more familiar territory on 66 minutes as he put Newcastle 3-1 ahead, latching on to Owen's pass and firing a low shot past Carroll.
The Hammers were not dead and buried, however, and Harewood's cool penalty after Ameobi's blatant handball set up a tense finale.
West Ham were suddenly on the front foot and Newcastle looked edgy although Carroll saved well from an Owen header, to deny him a hat-trick.
A frantic finale ensued, but with the Hammers pressing forward in numbers, a Newcastle counter-attack saw Owen set up by Amdy Faye's pass and the striker had an easy chance to slide home and complete his first Newcastle hat-trick.
West Ham boss Alan Pardew:
"There is a lot of consolation for us in defeat because in a structured way we have controlled the game.
"We made a couple of mistakes and their deadly duo of Michael Owen and Alan Shearer made us pay.
"But I could not fault my players and we showed some great technical ability. On another day we could have won 3-1."
Newcastle boss Graeme Souness:
"Once we get everybody fit we'll create even more openings for him. We've missed Michael when he's not been in our team through injury.
"He was the difference today along with Alan Shearer - who is still a very big player for our team.
"We felt the two of them today could get at West Ham's back four and score."
West Ham: Carroll, Repka (Aliadiere 76), Ferdinand, Collins, Konchesky, Benayoun (Bellion 62), Fletcher, Mullins, Etherington, Harewood, Zamora.
Subs Not Used: Dailly, Newton, Hislop.
Booked: Carroll, Ferdinand, Etherington.
Goals: Solano 20 og, Harewood 73 pen.
Newcastle: Given, Ramage, Boumsong, Bramble, Elliott, Solano (Bowyer 79), Faye, Parker, Ameobi, Shearer, Owen.
Subs Not Used: Harper, Luque, Chopra, Gate.
Booked: Bramble, Ameobi, Parker.
Goals: Owen 5, 43, Shearer 66, Owen 90.
Ref: P Dowd (Staffordshire).