How to make MacBook automatically connect to different wifi networks as desired?
OS X will connect to the Wi-Fi network with the strongest signal, although it won't switch immediately between networks to avoid disruption to the connection.

You can alleviate this by having the same SSID for both networks, which will cause OS X to see them as a single network and switch between them automatically.