The Minstermen started the day with just 17 points from 26 games and needed to pick up points quickly to keep in touch with those around them.
Victory was not enough to take York off the foot of the table, though, and they are level on points with Dagenham.
Kenneth McEvoy put the hosts in front on 15 minutes but their victory hopes looked to have been dashed when Keith Keane equalised with 10 minutes remaining.
But up popped Daniel Galbraith with a stoppage-time strike to delight the Minstermen fans.
Boss Jackie McNamara said: “It was a great time to score and a fantastic finish from Danny.
“He has the ability to get into those positions, find space and go past players and I was delighted with his response coming on. He was the reason why we got the equaliser last week and he’s been different class since coming here.
“He’s been our nearly man, as I’ve said before, so I’m delighted he got the goal. We brought him on because we needed a bit more energy in midfield as we were starting to drop a bit deeper, although they were not threatening us before the long-distance strike.”