In team games
In a team game, your job as a sniper is to support your team by protecting a single important area. Your team will rely on you taking out any enemy in that spot, so they can advance freely. At the same time, you have to rely on your team to protect the ways the enemy will go to take you out. It is impossible to protect yourself alone, as there is always a way to sneak up on you (unless the map is badly balanced), and neither should this be your goal.
If you are surprised by an enemy as a sniper, your team always made a mistake first. For instance, did you know where exactly your mates went, and which of your mates were taken out? Combine this with knowledge about the map and you can immediately see which ways opened up for your opponents to go. This allows you to predict where enemies are going to come from. If you are experienced, you can also accurately estimate the time when they'll appear (this works exceptionally well on higher levels, just watch a pro replay).
But even if you are not able to read into your enemy's movements (likely the case for most casual players), make sure to be alert, i.e. listen carefully - especially bad players will continue running even though they could be sneaking up to you from behind. Once you hear a sound (and know by the map that it's not from your teammates) just turn around and expect the enemy with your primary weapon in zoom mode.
If you can't take out the opponent in 1 shot, switch to your secondary weapon. Use the environment to your advantage (duck behind crates etc). If possible, throw a grenade to restrict his movement. Nobody expects a sniper to hold his liquor in a melee fight, so do what you can and be happy if you survive.
1. General strategy
In deathmatch, on the other hand, it is difficult to defend as a sniper, so first of all you should try to have a high rate of kills to outweigh your high rate of deaths. This strategy means you will be in a dangerous spot most of the time. Instead of thinking about defending, think about killing.
The correct place to be is part-hidden (i.e. behind a wall, in a house or on high ground) so you are difficult to hit from far away. At the same time, you absolutely need a good view on the ongoing action - as many possible targets as possible (you have to be able to hit all of them quickly, of course - a sniper that misses every second shot is not really worth his salt yet and should practice aiming first).
2. Looking both ways
Now that your location is very offensive, you will obviously be attacked by those who noticed you (i.e. everyone with a basic sense of direction). You should, as stated above, listen carefully to hear nearby steps. Also, look around every 0.5 seconds. If this sounds weird, let me explain the underlying concept first:
Regularly look around for incoming enemies for a split second, and be prepared to kill them off immediately. If you just fired a shot from your primary, you obviously can't hit them right away, so switch to your secondary and hope to survive. Else (if your primary weapon is ready to be fired) just hit them. You need good close combat sniping skills for that, but that's easy to practice.
So basically your strategy is to be on lookout 95% and on defense 5% of the time. This requires you to act very quickly if you want keep the timeframe under half a second, but it pays off in that you are extremely dangerous for everyone in your field of view and difficult to surprise from behind at the same time. The surprise-timeframe is only 0.5 seconds if you do it correctly, so you have a fair chance to defend yourself even against skilled opponents.
3. Why it works
Your advantage is that your intruding opponent will always appear at a position that you know (e.g. a door) so you can aim at him without thinking or looking, just move your mouse in the position (takes less than 0.1 seconds), realize if someone is there (less than 0.1 seconds) and hit it (0.1 seconds, depends on your body). So on average, your time until reaction is only about 0.2 seconds all in all if you are in defense mode and 0.35 seconds on average if you are in offensive mode.
Your opponent, on the other hand, moves into a room without knowing where exactly you are (I hope you're not always directly behind a window!). He has to look for you, aim at you and hit you. The look and aim part takes a skilled opponent about 0.2-0.3 seconds, which is comparably close to your reaction time. This means you have a high chance to finish him off before he gets you if you are in defense, and a reasonable chance if you are in offense.