Both sides are battling for a top four finish and the win sends United above West Ham with a game in hand.
"If you score in the last second of the game that is the best type of wins, a clean sheet. I think in the end we deserved to win. It takes patience to play West Ham. A few moments at the end I wished we would have passed the ball instead of shooting from wherever," United coach Ralf Rangnick said after the match.
"Bearing in mind we played a few days ago against Brentford, the team looked physically good. We knew it would take patience, the way we played defensively, won balls back, I was really happy with that."
Ronaldo had a penalty call turned away 15 minutes later when Kurt Zouma brought him down in the box.
Jarrod Bowen had an opportunity to put the visitors ahead early in the second half when he brought the ball down onto his chest on the edge of the box but his shot went into the side netting.
There was a VAR check after the goal to see if Cavani was offside in the build up but the goal was allowed.
"It is obviously an important goal and such late on is an amazing feeling. We have to use that winning feeling. We could have been a bit better on the ball but managed to get the goal we needed and can use that as motivation now," Rashford said after the match.
"When there are two teams so close together in the table, both are fighting to be in front of one another and today we managed to get there. We are sitting fourth now, some teams have games to play, but it is nice to be in that position.
"The wins are what's important, when you get wins, consecutive wins, you can really start to go on a run and that is what gives everyone confidence.
"We have had a difficult week, there have been many games in one week, so the break is needed. It'll be good for us."
Rangnick said that Martial didn't want to be part of the squad for the Aston Villa game last week and the 26-year-old hit back on social media saying he would never refuse to play for United while at the club. However, the coach did say that the pair had had a conversation and sorted out their differences.