On an emotionally-charged night in east London, it looked as though Anthony Martial's double had ruined the Hammers' party but late headed goals from Michail Antonio and Winston Reid secured the win that almost took the roof off the old stadium.
As the home team lapped up the applause and fans broke into a stirring rendition of their 'I'm forever blowing bubbles' anthem, United's players trudged off knowing their Champions League qualification hopes are no longer in their own hands.
"In the last match of the season we can pass over Manchester City, they can lose and we can win -- then we are fourth, you can count I think?" United boss Louis van Gaal told reporters.
For United to finish fourth they have to beat Bournemouth in their final match of the season while Manchester City must lose to Swansea.
Van Gaal also refused to blame an attack on the team bus by West Ham fans for his side's defeat, though his players looked nonplussed as they were pinned back early on in the Boleyn cauldron after a 45-minute delay to kickoff.
"We know it can happen so we have to cope with that. Twenty minutes from the end, we were 2-1 ahead so I don't think that was an influence," the Dutchman said.
While United's need was probably greater, West Ham were desperate to leave the Boleyn on a high after a 4-1 drubbing by Swansea City at the weekend.
They went ahead after 10 minutes when Diafra Sakho swept the ball home from Manuel Lanzini's cut back, then Andy Carroll spurned a great chance to make it 2-0 and Antonio had a goal ruled out.
United sent on former West Ham midfielder Michael Carrick after the break to try to instigate a comeback.
The visitors did improve and were level after 51 minutes when Marcus Rashford and Juan Mata combined to set up Martial.
Dimitri Payet and Sakho went close to putting West Ham back in front but United struck on the break when Martial's shot embarrassed goalkeeper Darren Randolph at the near post.
The visitors' lead lasted four minutes though as playmaker Payet's floated pass picked out Antonio, who thumped a header past United goalkeeper David de Gea.
With the atmosphere building to a crescendo, West Ham snatched victory in their 2,398th and final game at Upton Park when Reid's header beat De Gea's despairing attempt to keep the ball out.
The roars would have been heard down the road at the Olympic Stadium where sixth-placed West Ham could host Europa League matches next season if they finish with another win at Stoke City on Sunday.