The Baggies went on to break the deadlock on the half-hour mark through James McClean, who marauded into the area past Winston Reid before firing a shot which took a deflection off Pedro Obiang and looped into the back of the net.
However, the Hammers drew level on 59 minutes when Aaron Cresswell's whipped cross from the left was headed home by Andy Carroll, who rose high above Jonny Evans and Kieran Gibbs to get his first goal since April 1.
Less than 10 minutes later Marko Arnautovic nearly grabbed a second, the Austrian linking well with Lanzini and getting into a promising position, only for Foster to keep out the ensuing effort.
Just as it seemed like the game would finish all square, the Hammers struck a winner in the fourth minute of injury time, Arnautovic cutting the ball back for Carroll to slam home from a tight angle.
West Ham United (3-4-2-1): Adrian; Reid, Ogbonna, Cresswell (Rice 76'); Zabaleta, Masuaku (Chicharito 73'), Obiang (Noble 46'), Kouyate; Lanzini, Arnautovic; Carroll
West Bromwich Albion (4-4-2): Foster; Dawson, Hegazi, Evans, Gibbs; Livermore (Robson-Kanu 64'), Yacob (Barry 83'), Krychowiak, McClean; Rodriguez (Burke 71'), Rondon