Alejandro Garnacho curled in a 90th-minute strike past Alphonse Areola, before Fred steered in an injury-time third goal to put the hosts into the last eight and see off the battling Hammers.
West Ham scored first early in the second half through Said Benrahma's powerfully-struck shot which flew into the top corner past David de Gea.
The home players had thought the ball had gone out for a throw-in, with Diogo Dalot and Antony both stopping.
That allowed the space for Benrahma to exploit and score, with a video assistant referee (VAR) confirming Tomas Soucek had kept the ball in play.
United thought they had equalised through Casemiro's header - only for it to be ruled out for offside after a VAR check.
But the home side did manage to equalise in the 77th minute when Nayef Aguerd, under pressure from Wout Weghorst, headed into his own net from Bruno Fernandes' excellent corner.
However, Garnacho, who also scored a last-minute winner in United's 2-1 win over Fulham in November, was the Old Trafford hero with a fine finish to put his side ahead. Fred added a third with virtually the last kick of the game.
The home fans chanted about going to Wembley at full-time and they are only one game away from returning back to the national stadium for the semi-finals next month.
On Sunday they beat Newcastle 2-0 in the Carabao Cup final to pick up their first piece of silverware since May 2017, with the trophy held aloft pre-match by club captain Harry Maguire and Bruno Fernandes.
However, there was little fuss to the ceremony, a sign that the club's targets lie higher than the Carabao Cup.
And United, who only just scraped into the Europa League by finishing sixth last season, are still fighting for every trophy in 2022-23.
The 12-time FA Cup winners are also in the last 16 of the Europa League and third in the Premier League, although 11 points behind leaders Arsenal after the Gunners thrashed Everton 4-0 on Wednesday.
They were 13 minutes away from losing to West Ham, before their impressive late rally took Erik ten Hag's side through to a home tie with Fulham, with the game to be played on 19 or 20 March.
Said Benrahma was impressive as he opened the scoring for West Ham
Ten Hag made six changes from the side that beat Newcastle and his side started brightly. Marcel Sabitzer nearly scored his first goal following his loan move from Bayern Munich, but was denied by Areola's excellent low save.
But West Ham, 16th in the Premier League and two points clear of the bottom three, were the better side for the rest of the first half. Michail Antonio missed a great chance when he raced onto Benrahma's excellent through-ball, but De Gea made a crucial save.
The Spanish keeper, presented with an award by former boss Sir Alex Ferguson before the game after his 181st club clean sheet in the win over Newcastle broke Peter Schmeichel's record, was in excellent form as he also denied the impressive Benrahma and cut out Antonio's dangerous cross.
Ten Hag had to do something at the break and brought on Casemiro, who had been named in the Fifpro World Team of the Year for 2022 and scored the opening goal at Wembley.
The Brazilian nearly made an instant impact as he played in Weghorst, who fired over the top.
West Ham then took the lead with Benrahma's 54th-minute goal - and that proved the instant signal for United's 25-goal top scorer Marcus Rashford to come off the bench.
David Moyes' side had chances to double their advantage but Pablo Fornals and Antonio failed to take two of them, before the hosts piled on the pressure.
Casemiro headed in from a Fernandes free-kick, only for it to be ruled out, but another set-piece from the Portuguese international set up the own-goal equaliser.
West Ham were hoping to hang on for extra time, but Garnacho's curled finish, followed by Fred's goal in the fifth minute of added time ended their hopes.
-- Courtesy of BBC Sport