Ads are based on your google search history, clicking behavior and also where you have been on the web. Virtually all major sites store third party cookies that allow third party data mining entreprises to follow you and try to determine what ads are you likely to click on.
What is beyond my understanding here is that following this logic, people that click on internet ads actually exist, as quite an industry is developping based only on that demographic.