The visitors took an early lead when Glen Johnson cut in from the right wing and fired a venomous shot past West Ham goalkeeper Jussi Jääskeläinen from the edge of the area.
However, the Hammers fought back when Mark Noble scored from the penalty spot after Joe Allen’s handball, before Steven Gerrard netted an own goal in the 43rd minute.
Liverpool, without the suspended Luis Suárez, equalised through Cole’s angled finish before James Collins diverted the ball past Jääskeläinen under pressure from Jonjo Shelvey with 11 minutes remaining.
“It’s an important win for us because West Ham were only three points above us,” ex-Hammers midfielder Cole told Sky Sports.
“It’s always nice to come back to your old club and it was a good win for us. I’ve scored a few goals against West Ham and I’ve never celebrated.
“But the most important thing was getting the win.”
Shelvey, who played a role in Liverpool’s winner, was delighted to help the Reds secure three points in the absence of talisman Suárez.
“I’m going to claim it,” said Shevley. “I think I touched it but the main thing is that we got the three points.
“We’ve got a few games coming up now so it’s important we kick on now.
“I’ve played it [the striker role] against Young Boys, so I’m not a stranger to the role. But it’s not about me but the team – and the team were outstanding today.”