Without a win away from home in the league since August, the side now managed by Graeme Lee had to withstand a lot of pressure at times against a side eager to enhance their promotion prospects, but did so very well and took full advantage of defensive mishaps.
Edimilson Fernandes tested Aynsley Pears with a shot from distance early in the game and was instrumental in the Hammer's next threat when setting up Daniel Kemp to cross dangerously, Pears again doing well to clear.
In between, Boro had claims for a penalty turned away when Alex Pattison fell under challenge.
Ashley Fletcher headed just over as the home side kept up the pressure as Fletcher headed just over and Joe Powell shot at Pears, but it was Boro who made the breakthrough two minutes into injury-time when James Husband cracked a free-kick from the D, a deflection left Nathan Trott with little chance in the West Ham goal and Boro, on the back foot for a lot of the half, went in one up at the break but not before Kemp forced another good save from Pears.
The woodwork came to Boro's rescue early in the second half when Powell meet a Kemp cross and headed against the inside of the post.
Prior to that, Boro had started the half well and edged further in front on 65 minutes when the Hammers made a hash of clearing a free-kick, Dael Fry caused all kinds of problems and Hayden Coulson was on hand to tuck in from close range.
It took just eight more minutes for Boro to wrap the game up as again the hosts failed to deal with a free-kick and Marcus Tavernier cracked home.
West Ham stuck to their task and scored a fine consolation goal on 82 minutes when Anthony Scully finished in style, but this was Boro's night.
The result leaves Boro in 10th and the Hammers clinging on to the last Play Off spot with one game to play. Boro have two games remaining, both at home, to the side immediately above West Ham in the table Wolves (the Hammer's final opponents) on Monday afternoon at Mowden Park, Darlington, and Newcastle United at the Riverside four days later on Friday 21st.
West Ham Trott; Nordtveit (Scully 61), Burke, Rice, Westley (Powell 20), Makasi, Fernandes (Sylvestre 46), Knoyle, Lewis, Kemp, Fletcher. Subs Forde (gk); Akinola.
Boro Pears; Johnson, Fry, McGhee, Husband, Liddle, Coulson (Mondal 83), Pattison, Tavernier, Lawson, Fewster. Subs Hemming (gk); Elsdon, Maloney, Wheatley.