Ayew scored West Ham's second goal after Pedro Obiang's inch-perfect pass allowed Sam Byram to tee up the Ghana international for the equaliser.
Norway international King did not have the best start to the game, missing from the penalty spot after Sofiane Feghouli had hauled Charlie Daniels to the ground in the Hammers box.
To make matters worse, the visitors took the lead less than a minute later, Michail Antonio sweeping a low shot past Artur Boruc into the bottom corner.
The host equalized after King flicked the ball over Jose Fonte's head with his right foot, before then drilling a low shot past Darren Randolph with his left.
Both sides headed into the dressing rooms level at the break, but within three minutes of the restart, King slotted home with a well-taken sidefooted effort to put the Cherries ahead.
Andre Ayew had looked to have earned West Ham a draw with a placed finish in the 83rd minute, but King sealed his treble at the death when he rifled into the roof of the net after Randolph had denied substitute Jack Wilshere.