I think a key thing as spy is to avoid groups completely. If you reduce the chances to bump into someone then it will make life easier.
I think spies work best when they are trying to pick off the people at the back of the lines, whether it's people running from spawn, or engineers or people going back for health. Kill, cloak, hide.
If you're in a situation where, when cloaked, you're trying to get through a group of enemies, either following, or running through the front line to get behind the enemy, then I highly recommend jumping from side to side. Being able to reduce the area of possible contact with an enemy is the key.
When disguised there is a little bit of timing involved, as well as a clear plan. You need to look like you have a definite movement, nothing gives away a spy as someone just running around after another person. If you attack when they are attacking then it's easier to blend in and not look suspicious.
I do my attacks as spy in a forward motion. Cloak, get in behind the lines, disguise, decloak, run forward with the enemy attackers and attack. No matter the result, I push through to my team-mates, heal and resupply. Rinse and repeat. Obviously if your using the cloak and dagger or dead-ringer then it's different.
There is a huge slice of luck involved with tracking and pushing through as a spy as people are a little unpredictable and sometimes you just get unlucky.
In order to avoid it I suggest to not try entering more than once in the door of specified team, if the first time fails selected random.
Anyway, if it happens that you got stuck, you can easily recover from this bad situation by opening console and write
retry
Retry command force a server reconnection and you access again the team selection screen. Sometimes, on busy servers with many people try connecting, this command causes to lose the slot.
For this reason, as suggested by @Dave McClelland in comments, is preferable use the command
spectate
because it fix the problem without forcing a re-connection.
If you really want to join RED or BLU team and reduce the probabilities to trigger that annoying behavior, I suggest to wait that half or more users on the server choose their own team, and then try enter your desired door.
Best Answer
If you right click the header, you can unhide the 'Bots' column. A regular server with bots will then advertise its bot count (unless there are enough humans playing; then it's not an issue right that second):
Be aware that some servers do not advertise their bot count at all, which is what you might be experiencing. If you're playing on Egypt in stage 3, you'll be able to easily tell humans from bots running against the wall; otherwise it can be quite hard to tell those bots apart.
What you can try is logging in at an unusual time where hardly anybody is online, like European morning. Those servers with 23/24 players (or similar) are probably entirely bots; go ahead and blacklist those. They're typically 24/7 servers or idle "trading" servers.
Sometimes I wish servers weren't this moddable.