So as a medic, I have a lot of people trying to backstab me. Which means I've had a lot of experience avoiding being backstabbed, so I thought I might share some tips with everyone (especially since we have some really good spies who play here), not in any specific order.
1) Look up. I can't stress this enough, LOOK UP! Most good spies will drop down from a higher level to stab you, ESPECIALLY since the hitboxes for a backstab are borked when coming down from above. So if you are standing where there are parts of the map directly above you, watch your back. And this leads to #2.
2) Know where "spy loops" are in maps. What's a spy loop you ask? It's a section in the map where a spy can drop down from above (usually), backstab 1 or 2 people, cloak and run back up to the original drop down spot and start over. The section after the first cap point on Upward, where the cart goes underneath into that tunnel, is a perfect example of a spy loop.
3) Watch players running the wrong way. Sure, that pyro could just be coming back for ammo, but then why isn't he running towards the ammo pack/dispenser? And since really good spies learn to act like they are on your team, staring at them freaks them out. In my experience, if you stare at a teammate and they notice you are starring at them, THEY ARE A SPY. If a person is running the wrong way AND heading towards you (this is harder if you are a medic), they are probably a spy. I mean why on Earth would a pyro who's headed backwards run towards a solider? Yeah, it's to backstab you...
4) Know what classes spies like to disguise as. Spy, Sniper, and Pyro are the top faves, but Demo is also very popular. Most spies pick a class that belongs towards the back of your team since that's where they want to be, so if that sniper isn't scooped in when he should be, I'd give him a few rockets to make sure.
5) Look behind the cart! How many times have you seen 3 or 4 people pushing the cart and a spy walks up and backstabs most of them? Probably more times than you can remember. So then you SHOULD remember to check behind the cart, but no one ever does. 4 people on the cart pushing and looking forward? Maybe one of those people should be looking behind the cart, ESPECIALLY if you've just capped a point.
Just remember, situational awareness is important.