In order to buy a Spanish registered car you need a residence card, a deed (escritura) or rental contract for a Spanish property, or a certificate of residence (certificado de empadronamiento) in Spain.
New car: Spain generally remains a buyers' market for new cars and you may be able to drive a hard bargain. New cars are sold at list price, although you should still shop around for the best deal as dealers compete in offering discounts, guarantees, financing terms and special sales.
2nd Hand: It pays to buy a used car that's around two years old, as depreciation in the first one or two years is considerable (high mileage cars, particularly rental cars, are good value). However, older cars in Spain (outside of their warranty period) aren't always well maintained. There are many cowboys in Spain selling old cosmetically improved cars so it's generally better to buy from a reputable dealer, even if you pay a bit more, and obtain a warranty.
When you buy a secondhand car in Spain you must obtain the registration document (permiso de circulation) the ITV test certificate plus the technical sheet and a photocopy; the road tax receipt and a photocopy; and a receipt for the payment of transfer tax. You have 15 days to register the vehicle in your name.
Importing a car: Importing a car into Spain involves a battle with Spanish bureaucracy, involving a lot of red tape. Most people employ a gestor (a type of administrator) to do it for them. The process has recently been simplified, but still involves mountains of paperwork and can take a number of months (and can cost up to around 600 Euros). The regulations vary depending on whether you're an EU resident or not.