Key to fighting snipers at long-range: shoot them constantly, and duck and move side-to-side unexpectedly.

Key to fighting spies as heavy: Watch your back for Pyros, Demos, Spies, and Snipers of your team coming up behind you. And also, listen and turn around when your team screams "Spy!"