I'm running macOS 10.12 after a factory reset, how can I upgrade to macOS 10.15 to run Xcode?
You are asked to install macOS 10.15 Catalina because macOS 10.15.4 is the minimum required version of macOS for running Xcode 12.2 which in turn the current latest version of Xcode available on the App Store.
The latest version of macOS Catalina is 10.15.7 which can be downloaded and subsequently installed via the Mac App Store by using this link:

macOS Catalina

There's a newer version of macOS, macOS Big Sur which was recently released by Apple and the same can be downloaded from the Mac App Store by using this link:

macOS Big Sur

Most Macs from 2013 and later support running macOS Big Sur, so your Mac should definitely be supported if you bought it brand new 3 years ago.
If you are looking to run an older version of Xcode, macOS Sierra 10.12 would support running Xcode 8.3.3 and if you have the latest point release  of macOS Sierra installed, i.e. 10.12.6, the latest version you can install is Xcode 9.2.
Mac App Store makes available the current latest public version of Xcode. If you are looking to download an older version, you can do so from Apple Developer Downloads website.
Refer to the Xcode - Version Comparison Table to determine the supported Xcode version information)
Each version of Xcode comes built in with a specific version of SDKs for macOS, iOS, watchOS and tvOS. If you are looking for SDK targeting specific version of any of the OS, refer to the Version Comparison Table linked above to determine the appropriate version of Xcode to use.