The striker was flagged offside as he prodded Martin Kelly's knock-down into the net three minutes from time.
But a lengthy VAR check on two potential offside decisions, for Kelly and then Ayew, went Palace's way by millimetres.
England midfielder Rice looked like he was hailing a taxi when the ball struck him in the area, and Patrick Van Aanholt took full advantage from the spot.
Then Ayew's intervention secured all three points for Palace as the Hammers missed the chance to climb back up to third in the table – with Palace instead moving fourth.
The hosts had soaked up some neat but ultimately unproductive Palace pressure before carving a gilt-edged chance for Haller.
Andriy Yarmolemko rolled Cahill down the right and squared the ball for Haller six yards out.
The £45million striker hit it first time but only succeeded in firing the ball straight at Palace keeper Vicente Guaita, who made a fine block.
Palace were inches from taking the lead in the 35th minute when Wilfried Zaha tip-toed around Aaron Cresswell on the byline.
Zaha's cut-back was clipped towards goal by Jeffrey Schlupp but Fredericks had spotted the danger and got back to hoof the ball off the line.
At the other end an Anderson cross almost created an own-goal, the ball ricocheting off two Palace defenders before bouncing to safety.
West Ham had to call on their back-up goalkeeper Roberto after an injury to Lukasz Fabianski last week and the Spaniard settled any nerves on his home debut by beating away James McCarthy's drive from the edge of the box.
In first-half stoppage time Yarmolenko and Anderson combined to tee up Manuel Lanzini, but the Argentinian drove wide.
The breakthrough came nine minutes after the interval following a superb West Ham move, starting inside their own half and involving eight players.
Cresswell and Mark Noble freed Anderson down the left and the ball was moved on to Lanzini, Declan Rice and Yarmolenko before reaching Fredericks, whose low cross was tucked away by Haller.
But Palace hauled themselves level just after the hour when former Hammer Cheikhou Kouyate's flick hit the raised arm of Rice in the area.
Roberto had the chance to make himself an instant hero but Van Aanholt, on spot-kick duties with captain Luka Milivojevic suspended, sent him the wrong way.
Palace won it after substitute Andros Townsend crossed for Kelly, who nodded down for Ayew to tuck in, with the VAR's lines on the television screen eventually ruling in their favour.