I just bought a new iPhone SE to replace a destroyed iPhone 5S. It was set up to the point of being able to make calls at the Apple store, but it was not logged into iCloud. At some point after getting home I noticed it was already logged into my router's WiFi. I had not yet manually attempted to log onto my router. I may have logged into iCloud but this device must have a different UDID than my old iPhone 5S. How then was my new iPhone able to automatically log into my router's WiFi?
iCloud Keychain shares your WiFi networks, including passwords, to all of your devices. So if your new phone had been logged into iCloud, that’s where it got the network details.
In addition to Mike Scott's answer, there is another reason why your WiFi network could have been working when you got home.
When the new phone was set up at the store, if the tech did a backup/restore between phones or otherwise transferred any of your old phone's data over to the new one, then settings would have been transferred as well. In this case, WiFi SSID credentials would typically be part of the transfer process and that would explain why your network was joined when you got home.