There is a huge TF2 wiki page devoted to 'SpyChecking' including a rundown on how each class can identify a spy: I will copy some here for you.
Remember, just because you're not
paranoid doesn't mean the whole world
isn't out to get you...
- Remember that members of opposite teams cannot pass through each other. If you bump into a teammate and cannot pass through him, he is definitely a Spy.
- In general, if you see a teammate behaving oddly, there is a likely chance that it is a Spy.
- Players in the wrong locations (such as a Pyro in the rear or a Sniper or Engineer on the front lines), deliberately avoiding contact with teammates, or hiding in corners can all be enemy Spies.
- If you see your own name yell a voice command that you didn't use, it means that a Spy is disguised as you. Be sure to immediately warn your team of this, as well as your class so that your teammates know what class to look for.
- Look for telltale shrouds of smoke that are colored red or blue. When a Spy uses his Disguise Kit, it takes a second for the disguise to equip, during which the Spy is shrouded with smoke of his team color. While the smoke does not appear if the disguise is used while the Spy is cloaked, it will follow the Spy for a short distance if he uses it before cloaking.
Just running into everyone (since the Scout is the fastest class) does the trick; as teammates can clip through each other, any teammate you cannot pass through is an enemy Spy in disguise. Also, Scouts using the Sandman can stun disguised Spies, while the baseball will pass through friendlies. Throwing Mad Milk at disguised Spies will expose them, allowing you and your team to easily hunt them down. Alternatively, two hits from the Scout's primary weapons will kill a Spy not equipped with the Dead Ringer, but it's less economic than simply running into them. You can also use a single blast from the Force-A-Nature at close range and see if a teammate is knocked back by it; Spies will be severely hurt and sent flying by the blast.
There is nothing particularly special the Soldier class can do to Spy-check. The limited amount of Spy-checking a Soldier can do consists of sending a rocket into teammates to see whether the teammate is juggled by the blast or not, and occasionally pumping Shotgun blasts when it is not an inconvenience. Buff Banners and Battalion's Backups will not activate on enemy Spies, so that can be a giveaway. The rocket launchers have low ammo counts and you might not always notice disguised Spies when using the Shotgun, so generally it is a matter of analyzing behavior.
The Pyro's ability to set enemies on fire with incendiary weapons is perhaps the most useful for detecting Spies, since only enemies can be ignited. This is the most reliable method of Spy-checking teammates. The team-colored glow produced by a burning player is especially visible when surrounding a Cloaked or Disguised Spy.
Much like with the Soldier, the traditional Demoman is not an effective Spy-checking class. In general, your melee weapon is probably your best bet as you lack hitscan weapons. The Grenade Launcher has limited ammo, but is probably the most viable way to Spy-check teammates without using your melee weapon, as your grenades will detonate on contact with Spies. Placing sticky bombs with your Sticky Bomb Launcher takes time, but teammates that try to avoid the bombs can be easily identified as a Spy. A Demoman using the Chargin' Targe will stop when running into a Spy while charging, and said Spy will be an easy kill.
Heavies can attempt to Spy-check by firing their Minigun on suspicious teammates; a few seconds at close range will make quick work of an enemy Spy (make sure to have ammo to spare afterward). If a teammate eats your dropped Sandvich, and was already at full health, he is a Spy (unless he is a friendly disguised Spy, in which case he might have used it to 'heal' his disguise). A spun-up heavy is a tempting Backstab target, so even if you're busy mowing down enemies, try to find time to turn around and spycheck.
As an Engineer, Spies are a substantial threat, able to place Sappers while disguised, but they usually aren't trouble if you can get teammates to protect your buildings (as they often are, by Pyros and teammates who need healing or ammo, or yourself). If you receive an Electro Sapper notification while a teammate is standing near the sapped building, the teammate is a Spy. Still, it's advised to Spy-check everyone with your Pistol, especially teammates approaching your buildings. If any teammate is unable to pass through your Sentry Gun or Dispenser, that teammate is a Spy.
It is also a good idea to shoot around the immediate area with your Pistol or Shotgun to check for Spies using the Cloak and Dagger. If you hit a "teammate" with the Southern Hospitality and they start bleeding, then they are a Spy. The bleeding effect will also persist when they are cloaking as well.
Medics are high priority targets for Spies. Make sure you check before you start healing anyone, unless they already performed an action impossible for a Spy (e.g. shooting). If a teammate seems suspicious to you, make sure to spycheck (attempt to clip through him, hit him with your melee weapon, etc) before healing him. Remember to frequently watch your back, as Spies love to attack Medics who are already healing another patient.
Always watch your surroundings, and be wary of any teammate that attempts to approach you from behind.
The Sniper is very vulnerable to Spies while scoped, and is traditionally an easy Spy target, though they also have various anti-Spy weaponry that they can employ. Using Jarate on teammates will immediately reveal enemy Spies and render their Cloak useless, making them easy to identify and eliminate. Arrows from the Huntsman will also stick to disguised Spies, while simply passing through teammates. Carrying the Razorback will allow you to survive a single Backstab, and you can use this to your advantage by goading a Spy into attacking you, though most experienced Spies will usually resort to their Revolver when faced with such a Sniper. The Tribalman's Shiv makes Spies bleed, which can partially disable their cloaks and expose them to everyone.
Even the Spy can do a great job of Spy-checking. Stab all of your teammates in the back; if you are lucky you may stab an enemy Spy. You will find your character raising his knife (just like when about to backstab an enemy) when standing behind a disguised enemy Spy. Also, you can Cloak and sneak around the map to alert your team of any incoming Spies. It's also a good idea to roam around your own base while disguised as an opposing enemy, as enemy Spies will rarely try to Spy-check you if you can act well.