First learn these to binds: ("disguise") --- allows you to change your skin and color. ("feign") --- allows you to play dead.
These are the two most important weapons for any good spy. The first command is used to change your skin/color to infiltrate the enemy base. First change your skin to an acceptable class, one that matches your speed. Don't try and be a HWguy flying by all the snipers and soldiers with Spy-like speed. Try a scout, engineer, or medic. Don't have your axe out unless your a medic or engineer, or else that will raise suspicion also. Your axe does 6 times the damage if you stab someone in the back, so use the medic and pretend to "heal" someone and give them a nasty suprise! Your hypodermic needle from the single shotgun is also useful. It slows down the opponent by like half-speed, preparing them to get bucked up by your teammates. Use your grenades (concussion, hallucinogen) to wreak havoc on enemy bases, also keep yo team informed about the opposing defense.
AGAINST
Scouts can always detect a Spy through touch, but you can always use the "shotgun test". This test consists of shooting a suspicious "teammate" with a shotgun shell. A Spy will always grunt like an enemy hit with a shot, once detected, blast that fool into pieces. Don't use rockets though, cuz the suspect may really be a teammate. Watch out for particularly strange things, like teammates leaving a battlefield, Snipers facing the wrong way, Soldiers moving fast, Pyros carrying axes, etc. Also, although somewhat cheap, use the score list to see if the enemy has any spies. Check if there are any discrepencies between a player's TEAM and COLOR. If there is, you know the enemy has a Spy.