When I'm out and about and there are open wi-fi networks available, my phone won't automatically connect to one. I have to go into wi-fi settings and select it. And I don't see a setting that allows to automatically connect to any open network. However, if it's a network that I've connected to before, it will remember those and then automatically connect when it's in range.
If you're automatically connecting to a network that you don't want to, go to wi-fi settings and it'll show you the list of available networks. Long press on the one you don't want and an option will pop up to "forget network". Then it shouldn't automatically connect next time it's in range.
You may also look at an app called Tasker. I've read that you can configure it to turn wifi on or off when you come into or leave the range of preferred networks. For example, automatically turn off wifi when you leave your home then turn it back on when you get to work. I've never tried it, but heard it works pretty well.