If you've done your homework, and you're buying a new gun, you really shouldn't have too much to worry about. Most are going to be covered under warranty, so if you do get a lemon, the company should make it right. Basically, as long as you've done your research, and you're buying a gun with a good reputation made by a company with a good reputation, your chances of getting the proverbial shaft should be pretty low.
Buy used is a bit more of a gamble. Most used guns at your LGS will likely have been checked out by the gunsmith. A reputable LGS won't sell anything they think is low quality, damaged, or whatever, without mentioning the problems. Buying online or from a third party, as a newer person can be somewhat problematic. The best thing to do is to bring someone along that knows what they're doing. Eventually, you'll get better at knowing what you're looking at.
My recommendation, and this is only for a newer shooter, is to only buy guns from a reputable LGS. Don't buy third party, or used online, until you know what you're doing. Although problems are going to be rare, I just wouldn't risk it.