This is done in many ways, say you open a store selling bycycles a marketing company can contact the credit card companies that people have used to pay for the bikes at your store and get alot of demographic information that way. They can also have someone observe your store in person and see the age, sex and nationalily of those you frequent your store the most. Once they know who your customer is for example single male 17-35 income of 20-30k per year caucasion. then they know who to direct the add campaign towards. It wouldn't make sense to send discount coupons to woman over the age of 50 and married if they don't buy bikes.