Most of the banner ads you see here and especially ads you see when you're NOT logged in, are served by our ad networks. Those networks serve ads based on a number of criteria including your browsing history and/or what you're currently viewing on your browser.
Even then, the ad networks now use algorithms to expand their criteria to serve ads to us (not just on this site but on any site that uses ad networks like Google ads).
I remember I was browsing through a photography forum I belong to and I started getting ads for Russian brides. I figured it was because I was reading about tennis (huge tennis fan here) and well, they somehow connected the dots. That's my story and I'm sticking to it.