Castellanos tied the game at 2-2 in the 83rd minute with NYC's first goal from a header this season. It came six minutes after Mauro Manotas gave Houston a lead with a strike from 22 yards out for his ninth goal of the season.
Castellanos had another header blocked in stoppage time, but he knocked home the rebound for the winner.
Alexandru Mitrita opened the scoring for New York City (10-4-8) in the 16th minute with his sixth goal of the season. Juan Cabezas made it 1-1 10 minutes later by redirecting Tommy McNamara's cross at the penalty spot.
Romell Quioto received a straight red card for Houston (9-12-3) in the 29th minute for an elbow above the shoulders of Ronald Matarrita.
Castellanos's late heroics lifts NYCFC past Dynamo
Valentin Castellanos scored twice late in NYCFC's win vs. Houston. For more MLS, sign up to ESPN+.