The sniper is the only class that has significant long range capability. They're also the only class that can pull off proper headshots, which usually result in a one shot kill. Their sidearm, the greasegun, is a surprisingly effective weapon against unsuspecting players and classes that lack an effective medium range weapon. They also have one of the lowest health totals in the game, making them unusually hard to kill. The fact that you're looking down a scope for most of your game makes you a sitting duck for spies. You have no effective way to take out sentry guns that have been upgraded past level one.
On the other hand, there are few things that are sexier than the sniper. They require far finer aiming skills than any class, as the headshot is their most effective way to take down enemies. You should always try to aim for the head, if only to work on your aim. You need to stay zoomed in for a few seconds while your charge-up bar increases in order to inflict maximum damage, so it behooves you to spend extra time honing your aim.
Don't shoot every target that comes your way. It gives up your position, and will often result in more misses than hits unless you've already developed your skill. Choose the most important targets from the opposing team, like medics and easy-to-strike pyros. Move around frequently so that enemy snipers won't be able to predict your position. Be sure to lead your shots if you find yourself missing too often. Sniping is very ping dependent, so if you have latency over 75 ms, you should consider switching to another server if you're having too much trouble scoring headshots.
If you have a particular area staked out, however, it'll be hard for other snipers to take you out because they'll need to wait for their scope to charge before they can pop you in the face.
As the sniper, your primary job is to shock the other team by picking off crucial players when they least expect it. If you're particularly good, you can shut off entire avenues of advance, as the players will be too nervous about being picked off by you to use them.
If you are discovered - particularly by a player at close range - you should switch to your greasegun or machete and start retreating to your teammates. If you keep getting backstabbed, try to find a spot that you can place your back against.