Marcus Rashford scored the winner with virtually the last kick of the game as Manchester United leapfrogged West Ham into the Premier League's top four.
It seemed certain to be another frustrating afternoon for the hosts, who were stifled by the Hammers' organisation and otherwise came no closer than Fred's second-half effort that was saved by Alphonse Areola.
But with less than 30 seconds of the three minutes of injury time remaining, United broke and in a move that involved all three substitutes, Edinson Cavani held his run to remain onside as he collected Anthony Martial's short pass before rolling a cross to the far post, where it was converted by Rashford.
The result was harsh on the Hammers. In a game of few chances, Jarrod Bowen drove a shot into the side-netting before Tomas Soucek's glancing header bounced just wide of the far post with four minutes remaining.
But Rashford had the final word as United secured back-to-back wins for the first time under Ralf Rangnick.
The result takes United into the top four for the first time in three months, with the Hammers dropping down to fifth, although Tottenham and Arsenal could overtake them if they win their games on Sunday.
Marcus Rashford's goal was checked by the video assistant referee, who ruled that Edinson Cavani was onside in the build-upUnited's season stabilised
Such has been the difficulty of United's season, entering the rare January international break on the back of one defeat in 13 games since Ole Gunnar Solskjaer's dismissal on 21 November has to be seen as relative success.
There are still numerous problems for Rangnick to address, most immediately the futures of Martial, who was greeted with boos and cheers as he was introduced for the first time since Rangnick's arrival amid interest from Sevilla and his well-publicised desire to leave, and Jesse Lingard before the transfer window closes on 31 January.
A season spiralling out of control has been stabilised to an extent but this latest effort was unconvincing and another of those days when Cristiano Ronaldo failed to live up to his superstar billing.
The neck injury Rangnick revealed Ronaldo was suffering from in the build-up to the game eased sufficiently to allow the 36-year-old to start but there was little in his performance to suggest he was happy with his situation.
'Those are the best kind of wins!' - Rangnick happy after late goalAn elaborate tee-up to a 25-yard free-kick only produced a shot straight into the wall and then he moaned at Mason Greenwood for not crossing the rebound into an area he had ventured into rather than shooting, which is what the youngster did.
Passes went astray too frequently and a penalty claim was waved away by referee Jon Moss.
Ronaldo was still shaking his head minutes later but TV replays showed Kurt Zouma made little meaningful contact with the United forward, certainly not enough to send him flying in the box.
There was more elaborate complaining after half-time when he was penalised for a high kick on Craig Dawson, whose head, Ronaldo felt, was too low.
Rangnick stuck with the veteran though. And, with the rest of his team-mates, he was celebrating at the final whistle.
Rice unrewarded
Not easy to take defeat to late goal - MoyesOf England's Euro 2020 heroes from last summer, none has kicked on in their club careers the way Declan Rice has.
West Ham fans will hate it and, as Moyes points out, the 23-year-old has a contract that runs to 2024. But Rice is hot property.
When he strode from the tunnel first before kick-off, it was impossible not to think he might be doing that walk on a more regular basis in future.
His screening of the Hammers' defence was immaculate and the way he nipped in to dispossess Ronaldo, before racing away from his opponent as he tried to win the ball back, had a touch of impudence and authority in equal measure.
As Moyes' awful personal run of away games without a win at 'big six' teams extends to 45, Rice will know many of those clubs would take him right now.
Even with a European campaign to look forward to in the second half of the season, West Ham know keeping their skipper will be the biggest task of the summer.
Manchester United's next game is in the FA Cup, against Middlesbrough at Old Trafford, on Friday, 4 February (20:00 GMT). West Ham are in FA Cup action the following day, at non-league Kidderminster (12:30 GMT).
Player of the match
BowenJarrod Bowen
West Ham United
Squad number20Player nameBowen
Squad number41Player nameRice
Squad number4Player nameZouma
Squad number28Player nameSoucek
Squad number9Player nameAntonio
Squad number3Player nameCresswell
Squad number10Player nameLanzini
Squad number13Player nameAreola
Squad number15Player nameDawson
Squad number8Player nameFornals
Squad number5Player nameCoufal
Squad number24Player nameFredericks
Line-ups
Man Utd
Formation 4-2-3-1
1de Gea
20Dalot19Varane5Maguire27Telles
39McTominay17Fred
11Greenwood18Bruno Fernandes36Elanga
7Cristiano Ronaldo
1de Gea20Dalot19Varane5MaguireBooked at 56mins27Telles39McTominay17FredSubstituted forCavaniat 82'minutes11GreenwoodSubstituted forMartialat 82'minutes18Bruno Fernandes36ElangaSubstituted forRashfordat 62'minutes7Cristiano RonaldoSubstitutes
4Jones8Mata9Martial10Rashford14Lingard21Cavani26Henderson31Matic34van de BeekWest Ham
Formation 4-2-3-1
13Areola
5Coufal15Dawson4Zouma3Cresswell
28Soucek41Rice
20Bowen10Lanzini8Fornals
9Antonio
13Areola5CoufalSubstituted forFredericksat 70'minutes15Dawson4Zouma3Cresswell28Soucek41RiceBooked at 63mins20Bowen10Lanzini8Fornals9AntonioSubstitutes
7Yarmolenko11Vlasic16Noble23Diop24Fredericks26Masuaku31Johnson33Král35RandolphReferee:Jonathan MossAttendance:73,130Live Text
Match ends, Manchester United 1, West Ham United 0.
Second Half ends, Manchester United 1, West Ham United 0.
VAR Decision: Goal Manchester United - Manchester United 1-0 West Ham United (Marcus Rashford).
Goal! Manchester United 1, West Ham United 0. Marcus Rashford (Manchester United) right footed shot from very close range to the bottom right corner. Assisted by Edinson Cavani.Goal confirmed following VAR Review.
Attempt missed. Anthony Martial (Manchester United) left footed shot from the centre of the box misses to the left. Assisted by Bruno Fernandes.
Corner, West Ham United. Conceded by David de Gea.
Attempt saved. Cristiano Ronaldo (Manchester United) left footed shot from the centre of the box is saved in the centre of the goal. Assisted by Bruno Fernandes.
Foul by Scott McTominay (Manchester United).
Declan Rice (West Ham United) wins a free kick in the defensive half.
Attempt missed. Tomas Soucek (West Ham United) header from the right side of the six yard box misses to the left. Assisted by Jarrod Bowen with a cross following a corner.
Corner, West Ham United. Conceded by Bruno Fernandes.
Scott McTominay (Manchester United) wins a free kick in the defensive half.
Foul by Tomas Soucek (West Ham United).
Attempt missed. Diogo Dalot (Manchester United) right footed shot from outside the box is too high. Assisted by Scott McTominay.
Substitution, Manchester United. Anthony Martial replaces Mason Greenwood.
Substitution, Manchester United. Edinson Cavani replaces Fred.
Raphaël Varane (Manchester United) wins a free kick in the defensive half.
Foul by Michail Antonio (West Ham United).
Attempt missed. Bruno Fernandes (Manchester United) left footed shot from the centre of the box is too high.
Attempt saved. Michail Antonio (West Ham United) right footed shot from the centre of the box is saved in the top centre of the goal.
Our coverage of your Premier League club is bigger and better than ever before - here's everything you need to know to make sure you never miss a moment