Snipers seem to be one of the most frustrating classes to play against in TF2. As opposed to other frustrating events that can occur (backstabs, kritz demos, and backburner pyros, to name a few), when you see a sniper across the map, and he's looking right at you, you must rely mostly on his inability to aim. There's just not much else you can do!
Oftentimes, you can divide pub snipers into two main groups: the camping snipers, and the front line snipers. I'll summarize their playstyles, and how to beat them, below.
You know these guys. They're always safely away from the threat of spam, usually perched next to a sentry or a dispenser, and they're ALWAYS wearing the razorback. They're content to spend as much time as needed scoped in on one area, and generally don't move from there unless effectively countersniped.
This style of sniping is one of the most frustrating to go up against, because it often feels as if they have no skill whatsoever. They tend to bodyshot with high frequency, because they're relying on their reaction time to simply click on whatever comes across their crosshair.
To beat a camping sniper, you have few options. Typically, they are far enough away that mounting an assault with an offensive class is difficult, if at all possible. Additionally, the traditional counter, spy, is made more difficult because of the razorback, and the possible presence of teammates providing cover.
In this sort of situation, you just have to rely on your own sniping skills, or convince a teammate to countersnipe for you. If you can find an angle that leaves the enemy sniper exposed, without being visible, you can usually headshot them extremely easily, because they oftentimes will not move. Otherwise, move into their field of view, strafe madly, and hope for them to miss.
If you are an engineer, be careful for these players. They like to single in on you, because you typically have a superior "base" set up, and a single charged headshot can easily take you out. Use the wrangler to spam vantage points on your location, or hide behind cover better.
Front line snipers:
These guys are, for the most part, good at sniping. They tend to pop up on the forefront of pushes, trying to get key picks on medics and heavies. If you see them, you're in danger.
To beat this style of sniper, you can play pretty much any offensive class, with the possible exception of heavy. However, the key is to avoid letting them see you, as doing so reduces your likelihood of survival. As a soldier or demoman, use your explosive weapons to launch yourself above or behind the sniper, and try to move as quickly as possible in midair to avoid their tracking. The same applies to scout--double jump like mad, and never move in a straight line towards the sniper. As a medic, you're a prime target for a quick headshot, so constantly jump and duck when you realize that you're exposed, and, as before, avoid moving in straight lines.
Keep in mind that these snipers will often carry an SMG or jarate, to further bolster their team (they tend to be more team-oriented, as this comes with experience.) Avoid jarate like the plague, as you are a high target once the enemy players realize they will get mini-crits on you. Never underestimate the ability of the SMG to deal insane amounts of damage at close range, especially when critting.
If you want to get better as a sniper, you're probably playing as the corner camping sniper right now. Try to break out of that habit, and work on putting yourself neared to the "heavy" classes. Chances are, they won't notice you until you start popping heads like crazy. Forcing yourself to take more difficult shots will improve your aim and reflexes.
Simply lowering your sensitivity, and trying to aim as close to an enemy's head before scoping, will also greatly improve you aim. Lead your shots carefully, and read up on TF2 netcode (which will be covered later) to take the greatest advantage of lag.
Good luck, and steal lots of heads!