To avoid this mistake, make sure you only download from legitimate websites. Also whenever installing a program make sure you only select the features you actually want and not the unnecessary additions. This can be hard to do as software developers try and hide the tick boxes and optional choices from the unsuspecting end user. Anti virus software is not always capable of detecting adware installations and will allow you to override any warnings it may present. Games, free ring tones and video converters are amongst some of most common vessels of infection. Any download can be a potential vector for infection so it pays to use the legitimate app stores and websites. Drive by infections can also occur through malicious web page ads. There have been cases of popular websites serving up malicious ads (most recently MSN). These are rare occurrences though.
There are some things you can do to protect yourself: Make sure your programs and operating system are current; Install an ad blocker/privacy protector (my current favourite is Ghostery), and check for adware infections on a regular basis. My stock go to for this is ADWCleaner (downloadable from toolslib). For more persistent infections Malwarebytes is a good option. Lastly check your browser search engine settings. Make sure it is set to Google, Yahoo or Bing, anything else that you have never seen before should be deleted. The search engine settings can be found in your web browser options.
https://toolslib.net/
https://www.malwarebytes.org
https://www.ghostery.com