There are a few reasons.
To start: Microsoft's initial approach to mobile was fundamentally wrong. It shoehorned some of the desktop Windows experience into a phone (Start buttons, really?). It assumed that smartphones would primarily be for business. The dependence on styluses and D-pads was partly due to the lack of practical capacitive touch, but Microsoft wasn't even trying to move to this technology on its own. It wasn't until Apple had success with the iPhone that Microsoft finally acknowledged that finger touch and everyday users might be worth considering.
Which brings me to my next point: it was too slow to react to the iPhone. Like with BlackBerry, Nokia and Palm, Microsoft's leadership simply didn't understand how much of a sea change the iPhone really was. The moment Steve Jobs left the Macworld 2007 stage, Microsoft should have been planning a total overhaul of Windows Mobile and hiring hundreds (if not thousands) of engineers to make the new platform a top priority. Instead, it made excuses (see Ballmer's infamous "the iPhone has no chance" quote), grafted pseudo-touch experiences on to the existing software and generally took its sweet time responding. By the time Windows Phone 7 was available, it was too late: the iPhone had trashed Microsoft's share, and Android was a bigger player as well.
This was made worse by what happened next: a horribly inconsistent strategy. Forcing people to upgrade to new devices to get Windows Phone 8. Releasing a flagship phone one year but not updating it the next. And an overeagerness to give into carrier pressure for exclusives instead of insisting that the big new handset should be available on every major network. Simply put, Microsoft's/Nokia's overriding message was "we will abandon you." Why would you stick to Windows Phone knowing that you'll have to switch devices to get the latest OS, or switch carriers to get the latest device?
In a sense, there was one overriding problem: Steve Ballmer. He put too much emphasis on desktop Windows, and everything the company did had to revolve around that. Windows Mobile had to be a reflection of (and subservient to) regular Windows. The phone team at Microsoft could never be allowed to overshadow the desktop team. Unlike Apple, which was willing to shift from being a PC-first company to mobile-first, Ballmer clung to the PC world like his life depended on it. He eventually admitted to missing the boat on mobile, but it's wild to think that he never really took it seriously for more than a decade on the job.
And then there's Ballmer's seemingly sincere belief that everyone is biologically preprogrammed to love Windows -- he often thought that just having Windows was enough to sell something (see the initial Surface "it's a PC!" line). No, Steve, most people use Windows on computers because they either don't care or have no choice. He didn't realize that mobile was an entirely different realm, and that Microsoft's PC monopoly wasn't the sign of a manifest destiny to rule the technology world for all eternity. It only took one superior product to knock Microsoft off its mobile perch.