A great all-rounder, and thus default weapon, would be the Special Carbine. Usually, this will get you out of any sticky situation you'll find yourself in. To up your chances, mod it with a scope first and foremost, but other upgrades won't go amiss either. If the situation demands it, you can always switch weapons.
Learn To Snipe
You'd do well to put in a lot of practice with the sniper category, as these are some of the most useful weapons in the game in the hands of a skilled user. Cocking up a sniper shot might just mean that you've given the other guy a chance to send one through your skull, so get a feel for the guns as early on as possible.
Aim For The Head
A general rule-of-thumb regardless of weapon, range and perspective is always aim for the head. Always. The torso might be a bigger target, but with enough practice you'll hit the head enough times too and the damage multiplier makes it worth the effort. Taking out an opponent in fewer shots means fewer chances for them to take you out first.
Close Combat
While melee is a good way to go in close quarters combat, the assault shotgun we mentioned earlier is even better. Upgrade that thing with an extended magazine and you'll rip enemies to shreds at close range like there's no tomorrow. Here, accuracy isn't that big of a deal.
Don't Stand Still!
One major aspect of firefights that many players easily forget is movement. When you're in a scenario where both you and your opponent see one another and are at a reasonable distance, standing in one place if guaranteed to get you killed. That said, not any kind of movement will do, since if you keep a pattern to it the enemy will quickly pick up on it and start tracing their target. Be unpredictable.
Another useful tool in your arsenal is the combat or barrel roll. It can get you out of harm's way and behind cover quickly when used well, however another great utilization is to immediately roll just when your enemy stops their own roll. They can still trace their target when rolling, and this is a great way to throw them off balance. To do a barrel roll in GTA Online press aim (usually LT on consoles or right mouse button on PC) + X on Xbox, SQUARE on Playstation or Spacebar on PC.
A useful tactic to know is that if you perform a combat roll while aiming, it will break any target-locks that enemy players have on you. Also, while we're speaking of movement — don't forget that you can sneak. While slightly crouching is hardly the equivalent of going invisible, you are harder to spot, and present a smaller target.
Blind Fire
This seems counter-intuitive considering a couple of tips back we just said to aim for the head, but spraying and praying is more of a defensive move than an offensive one. This is basically giving yourself covering fire. It's less accurate, but has higher spread meaning there are more chances for hits, it breaks target locks and makes you harder to hit. When in doubt, empty your mag.
Good Luck
Soon you'll get a hang of these various techniques, and will automatically use them without having to think about it, and that's when you'll be in the best position to survive a firefight. Wasting time is the biggest killer in this game as it takes seconds for an enemy to kill you with explosives or a well-placed headshot.
Hopefully these tips, which were adapted from this thread, will help you stay alive for longer on the mean streets of Los Santos. Now, there's no need to go out and use these tips on unprovoked fellow players minding their own business — there are plenty of griefers in this game as it is.