Anwar El Ghazi put his side in front after 14 minutes, driving at the Heracles midfield with a mazy run before unleashing a long-range strike that flew into the top corner.
El Ghazi had the chance to put his side firmly in the lead just before the break, but the forward missed from close range.
The exciting winger tormented Heracles again after half time, with his shot well saved by Castro.
Soon afterwards, a deflected Gudelj free kick struck the bar before the resulting corner saw the ball crossed dangerously in the box but no Ajax forward could get on the end of it.
Amin Younes had an effort from range well saved towards the end as the Amsterdam side looked to hit on the break as Heracles pushed forward late on.
The win puts Ajax three points ahead of Feyenoord in second, while Heracles remain fourth.