The Red Devils came into the game on the back of an embarrassing defeat to the Young Boys in the UEFA Champions League in midweek. Ole Gunnar Solskjaer made three changes to the side that suffered defeat in Switzerland, with Mason Greenwood, Scott McTominay and Raphael Varane all handed starts.
The game started with both sides paying homage to former England international Jimmy Greaves, who passed on hours before kickoff.
Manchester United were slow out of the blocks and it was West Ham who asked all the questions in the early stages. Jarrod Bowen had two good chances to put the hosts ahead but saw his goal-bound shot blocked on the line in the 17th minute.
The second opportunity drew a smart save from David de Gea in the 29th minute but the capital side got a deserved breakthrough less than a minute later.
Bowen teed up Said Benrahma, whose goal-bound shot left De Gea flat-footed after it took a heavy deflection off Raphael Varane.
The goal sparked Manchester United into life and they drew level just five minutes later. Bruno Fernandes found an unmarked Cristiano Ronaldo in the box and his compatriot sent a shot at goal with a one-time flick that forced a save from Lukasz Fabianski.
The West Ham goalkeeper could, however, not hold onto the ball and Ronaldo was on hand to convert the rebound into the empty net. There were brief checks for offside but VAR Lee Mason gave the all-clear for the goal to stand.
Ronaldo had an excellent opportunity to put Manchester United ahead just two minutes into the second half but he shot straight at Fabianski from inside the box.
The rest of the second half sauntered on, with neither side really exerting their authority.
A dramatic end to the game saw goalscoring opportunities created at both ends of the field.
First, Jesse Lingard stepped off the bench to put his side ahead after he combined with another substitute, Nemanja Matic in the 89th minute.
The England international picked up the ball on the left flank and dribbled across the West Ham defense before sending an unstoppable howitzer into the top corner.
It was a drastic turnaround in fortunes for the 28-year-old whose injury time error gifted the Young Boys the winner on Tuesday.
Manchester United should have been awarded a penalty when Kurt Zouma felled Cristiano Ronaldo in the area. However, both referee Martin Atkinson and VAR official Lee Mason decided there was nothing untoward.
West Ham were awarded a penalty of their own at the other end after Luke Shaw's outstretched arm blocked Jarrod Bowen's attempted cross.
Atkinson initially awarded a corner but rescinded his decision after looking at the on-pitch monitor.
West Ham's long-serving skipper Mark Noble was immediately brought on for Bowen and he took responsibility for the spot-kick. The dramatic injury time gamble, however, backfired as the 34-year-old saw his effort clawed out with a wonderful save from David De Gea.
Incredibly, this was the Spaniard's first save from the spot in over five years and it helped Manchester United secure all three points. The victory continued the Mancunians' impressive record of picking up points from losing positions and took them joint-top of the standings with Liverpool.
Here is a rundown of how the Red Devils fared in the game against the Hammers.
Manchester United player ratings against West Ham
David de Gea - 8/10
The Manchester United goalkeeper pulled off a trademark save with his foot in the first half to deny Jarrod Bowen from close range. His most telling contribution, however, was his injury time penalty save to give his side all three points.
Luke Shaw - 7/10
The England international continued his recent improvement with another fine display down the left flank. Luke Shaw was placed on set-piece duties and created the most chances in the game (five). He was unlucky to be penalized for an innocuous handball in injury time.
Harry Maguire - 6/10
The Manchester United skipper made an error that was almost punished by Jarrod Bowen in the 17th minute. Other than that, he barely put a foot wrong for the rest of the game.
Raphael Varane - 6/10
Varane made a goal-line block to deny Bowen in the first half but was unlucky to see his deflection leave David de Gea flat-footed minutes later. The France international made four clearances and two tackles in 90 minutes.
Aaron Wan-Bissaka - 7/10
The Manchester United right-back was more progressive than usual and caused problems for Aaron Cresswell at both ends of the field. His final ball left a lot to be desired but he still put in a good defensive shift.
Fred - 6.5/10
Fred helped shield the Manchester United defense and made four tackles in the 87 minutes he spent on the field. He was replaced by Nemanja Matic with three minutes to go.
Scott McTominay - 6/10
The Scotland international made his first start in over a month but did not look out of place alongside Fred at the base of midfield. He had one shot off target and created two chances in the game.
Paul Pogba - 6.5/10
Paul Pogba was at the heart of Manchester United's dominance at the end of the first half as he combined well with Bruno Fernandes. His influence, however, waned after the break before his substitution for Jesse Lingard in the 73rd minute.
Bruno Fernandes - 7/10
The Portugal international put in another great shift and was the chief instigator of Manchester United's attacks. The 27-year-old made his presence felt with three chances created and two shots on target. He also created the Red Devils' first goal by putting in the cross for Ronaldo.
Mason Greenwood - 6/10
Mason Greenwood was largely on the periphery of the Manchester United attack for most of his time on the field. He was replaced by Jadon Sancho in the 73rd minute.
Cristiano Ronaldo - 7.5/10
Cristiano Ronaldo marked the 900th game of his illustrious club career with a well-taken equalizer in the first half. He was a thorn in the flesh of the West Ham defense and forced Fabianski into a couple of saves throughout the game.
Jadon Sancho - 5.5/10
Jadon Sancho came on for Greenwood in the 73rd minute but did not do too much of note in the time he spent on the field.
Jesse Lingard - 7/10
Jesse Lingard's wonderful solo effort one minute from time proved to be the winner and saw him go from zero to hero for Manchester United in the space of five days.
Nemanja Matic - 6.5/10
Nemanja Matic came on with only two minutes left in injury time but that was enough for him to make a mark as he provided the assist for Lingard's winner.