How does Android Marshmallow decide which WiFi network to automatically join?
There is this file /data/misc/wifi/wpa_supplicant.conf

Each Network you saved is listed there.

When multiple network blocks are listed in this configuration file, the highest priority one is selected for use or, if multiple networks with the same priority are identified, the first one listed in the configuration file is used.

(copy from http://www.freebsd.org/cgi/man.cgi?wpa_supplicant.conf(5), I think Android behaves the same)