Thomas Tuchel's side were in full control of the match for the full 90 mins but the Hammers were clinical -- scoring with almost every chance they get.
The Blues drew first blood at the London stadium when Thiago Silva scored with a header from a Mason Mount corner that goalkeeper Lukasz Fabianski could only punch against the post before it went in.
But the European champions allowed the hosts back into the game in the 40th minute when Jorginho put his goalkeeper Edouard Mendy under pressure with a poor back pass and Mendy brought down Jarrod Bowen for a penalty that was converted by Lanzini.
Four minutes later, Chelsea were back in front through Mason Mount who marked his return from wisdom teeth problems with a perfect volley from Hakim Ziyech's pass which caught out Fabianski at his near post.
Chelsea brought on Romelu Lukaku at the start of the second half to replace the injured Kai Havertz. But West Ham drew level again in the 56th minute when Bowen fired in hard and low from the edge of the box.
Masuaku turned the game West Ham's way in the 87th minute when he caught out Mendy with what looked like a cross but the ball flew into the near corner of the goal.
The defeat left Thomas Tuchel's side with just a one-point lead at the top of the table ahead of Manchester City, who visit Watford later on Saturday, and two points ahead of Liverpool who play away at Wolverhampton Wanderers.