1. Have an agency buy your car for you. You might not save as much as you could if you really knew what you're doing, but you won't get taken advantage of this way.
2. If you want to do it yourself, call dealerships up on the phone and compare prices. You might have a lower chance of convincing them to sell you the car than if you went in person, but you'll make sure you won't get ripped off.
3. If you really want to go to the dealer in person (I have used this option successfully), do your research. That doesn't just mean knowing the MSRP and the dealer's invoice cost (much lower than the MSRP), but also looking on edmunds.com or elsewhere for the current promotions (called "dealer incentives") that the manufacturer is giving the dealer. You might not be able to find all of their incentives, but you can get close. When you're at the dealer, don't let the salesman express prices in terms of monthly financing. They'll use the four square technique and rip you off with 100% certainty. First agree on a price and then you can talk financing with the F&I guys in the back. To get the price you want, start really low. Offer a number around the invoice cost and be willing to go barely more than $500 over (at least for non-luxury cars). When they tell you that they won't make any profit that way, you tell them about the dealer incentives you know about. If you still meet with resistance, you can even hint that you suspect they have other incentives you aren't even aware about and that you really will leave the store before buying a car for much more than the invoice cost. If they still won't budge, try again at a different dealership. If you have trouble with this method, switch to #1 or #2.