The 26-year-old has agreed a three-year contract with the option for an additional two years.
Ayew played a pivotal role in steering Swansea away from relegation last season, netting 12 times in 34 starts as Francesco Guidolin’s side finished 12th in the table.
He leaves Swansea one year after signing for the Welsh team on a free transfer from Olympique Marseille.
Ayew’s fee breaks the previous record of £15 million set when forward Andy Carroll joined from Liverpool in June 2013.
"I am very proud and happy to be at this magnificent club. I hope to make the people very happy here," Ayew told West Ham’s website (www.whufc.com).
"The club have a lot of ambition and have a great manager (Slaven Bilic). They want to become a bigger club every year and with the new (Olympic) stadium and a new training ground, you can feel they are moving on to another level.
"I want to train hard, do my job and show that I deserve to play for West Ham United. I want to give back to the manager and the fans the confidence they are showing me."