He is the first signing made by coach Slaven Bilic since the end of what was a difficult 2016-17 season, in which West Ham finished 11th in the table.
The Argentina international will join the club on July 1, once his contract with Manchester City expires.
“This is an absolute pleasure to be part of West Ham United Football Club,” Zabaleta told West Ham TV, with the club also claiming that he rejected offers from top clubs in England, Italy and Spain.
“I hope I can give you good moments. Of course, I come to the club to enjoy myself and hopefully we can have a very successful time.”
Zabaleta, who joined City from Espanyol in 2008, made 32 appearances in all competitions under Pep Guardiola this season.
The 32-year-old won two Premier League titles, two EFL Cups and the FA Cup with City.