It was bright start from both sides with decent chances from Sofiane Feghouli and Sam Vokes in the opening ten minutes. Vokes gave the hosts the lead 23 minutes in, before Fegouhli came back with an instant reply four minutes later.
The second period didn't have the same intensity as the first, but it was Slaven Bilić's men who snatched all three points with Ayew's strange winner rounding off their rollercoaster campaign with an impressive three points.
Kicking off in style
The trip to Turf Moor hasn't proven easy for anyone in this campaign, but it a fantastic start from both sides as they looked to send their respective fan bases into the summer happy.
It was the Hammers who had a glimpse four minutes in when Sam Byram put a great ball into the middle, but it was half cleared as it came to Feghouli. The winger took it down on his chest but Tom Heaton was down low to save well.
It was then the turn of the Sean Dyche's men as Stephen Ward played a great ball into the area, it looked destined to find Vokes but he couldn't get the required connection, instead rolling across the goal.
Anything you can do, we can do better
It was an even affair for both sides, but it was the home side who managed to put themselves ahead in the 23rd minute with a clinical move.
Andre Gray burst down the right and it was poor from the Hammers as he split their defence with his ball. It looked good for Scott Arfield but he completely missed the chance, Vokes was waiting in the wings behind him as he coolly slotted it into the bottom corner.
The visitors didn't let their heads fall as they equalised almost instantaneously, it was great piece of play from the Hammers as the returning Angelo Ogbonna found Ayew. The forward did brilliantly with an excellent flick that caught the Burnley defence napping, it came into the feet of Feghouli and he gave Heaton no chance as he buried his effort.
That goal started a great spell for Bilić's as the half began to trickle down, and Feghouli should have had two just after the half-hour mark. It was a good move from the left into the middle, rebounding off Matthew Lowton, coming to Feghouli, but he fired it over with the goal at his mercy.
Byram thought he had given his side the lead late on, as the full-back did well to fire it in from the tight angle but the flag was up for offside.
Ending the season in strange circumstances
There was little to talk about from the second period at Turf Moor, but Bilic's West Ham ended their season with a win following some strange, fortunate circumstances.
An attack looked to have broken down for the Hammers before it reached Edmilison Fernandes on the edge of the box. He hit a sweet effort which produced a smart save from Heaton. The ball was up in the air before coming back off the crossbar and falling onto the head of Ayew who nodded home from yards out.
The final chance of the campaign fell to Manuel Lanzini as he lined up a free-kick. The curling effort looked to have Heaton beaten but it curled just wide of the mark.