This type of targeted ad tracking is performed from within your browser. The good news is that you can prevent this; the bad news is that it's not terribly convenient (the convenient way to do it is to, well, just use the browser and pretend like you don't see the targeted ads, I guess). You can change your browser's privacy settings, but that will only do so much. You can also avoid using "single login" type credentials -- e.g. logging into any website that's not Facebook with Facebook credentials. If you really want to avoid tracking, I would stay away from Google services (Gmail, Google Drive, even Google Maps and Android) altogether.
You can also use browsers' "no-tracking" privacy modes and routinely switch browsers (rotate between several: Firefox, Chrome, Opera, IE...but realize that all come with varying degrees of tracking, even if you're in privacy mode. Routinely clearing your cookies and temporary Internet files will also prevent too many targeted ads, though this doesn't really stop tracking...it just stops you *knowing* about the tracking.