Angelo Ogbonna’s header on the stroke of half-time put the lowly Hammers ahead, but Nicolas Otamendi responded with a predatory finish shortly after the break.
David Silva won it for City, acrobatically converting a Kevin de Bruyne pass with seven minutes left.
Victory meant Pep Guardiola’s side re-established their eight-point lead over Manchester United, who they play next Sunday (16:30 GMT).
West Ham, though, have set a club record for the fewest points after 15 Premier League matches – they have just 10.
The Londoners had their chances – as well as Ogbonna’s goal, Michail Antonio almost pounced when Ederson spilled the ball, and Manuel Lanzini forced the goalkeeper to save at his near post.
But it always looked as though City’s pressure would tell, and shortly after De Bruyne’s free-kick was palmed away, Gabriel Jesus skipped through and slid the ball to Otamendi, who scored.
Leroy Sane, Raheem Sterling, De Bruyne and Jesus had further chances before Silva won it for the home side, but there was still time for the Hammers to go close, with Diafra Sakho shooting just wide after Marko Arnautovic pulled the ball back.