The first half was a drab affair with no goal action, but things came to life after the break, with Sebastian Haller giving the Hammers the lead in the 54th minute.
Patrick van Aanholt went on to equalise for the visitors nine minutes later.
Following a VAR check, the goal which was initially ruled offside has been awarded after it was deemed that Martin Kelly was onside when he headed the ball across goal for Ayew to tuck home#WHUCRY— Premier League (@premierleague) October 5, 2019
It looked like both sides were going to share the spoils until Ayew popped up for the winner three minutes from time, tapping in from Martin Kelly's knockdown ball. The goal was initially ruled out by the assistant referee for offside, but VAR corrected the decision.
It's Palace's fourth league win of the season from eight games, and the first time they've won back-to-back matches.
The result moves them up to fourth place, level on points with third-placed Leicester City.
Roy Hodgson's men have a date with Manchester City at Selhurst Park after the international break in a fortnight.