Why did GitHub sell their company to Microsoft instead of Amazon or Google?
Microsoft used to be the company that was well known for their destructive and absorbing business practices. That has changed thanks to this lovely man, Satya Nadella.
Satya Nadella has taken Microsoft in a completely new direction. Instead of focusing on selling products to consumers, he is focusing a whole lot more on Azure, which is selling products to developers. He has pushed open source efforts in the company to a whole new level, making Microsoft a prominent figure in the Open Source world.
One main focus is with their .NET platform.
By making .Net open source and cross platform, Microsoft is attracting hundreds of thousands of developers to the Azure ecosystem. You can develop .Net core and run it on anything, but Azure has the best tooling around for running it on the cloud. If you are using .Net, the default server hosting is Azure.
How does this connect to GitHub? Microsoft wants to continue to make themselves a prominent figure in OSS, and also make it easier for developers to use Azure. So, if they own GitHub, then they can make exclusive features only available for Azure users.
It is a common misconception on where Microsoft makes money. They do not make money off of windows (at least not very much). They make bucket loads of money off of MS Office and Azure.