The last year has not been a good one for Microsoft. Windows 8 kind of flopped and the Xbox One unveiling was a disaster to say the least. Microsoft's biggest problem, and this has always been their biggest problem, is they don't know how to market to consumers. IMO, Windows really only achieved dominance at home because it was so ubiquitous in the corporate world. This driving down hardware costs. Apple has never been cost competitive and nobody's really come up with a viable alternative to Windows that retains compatibility.
Of course desktop PC demand has been shrinking in favour of tablets and mobiles, which Apple dominates. OS X only has the same 7.5% market share that Windows 8 does, however iOS has 55% of the mobile market locked down.
Windows Phone has also been unable to crack iOS and Android's dominance over the smartphone market. It's in third place yes, but only at about 3.2% market share. Nokia is one of their biggest, if not the biggest hardware partner. So buying them out makes sense as it gives them control of the software and the hardware. MS is trying to play Apple's game. Mind you, this didn't work out very well when they did it with the Surface RT. It's not just pushing out a phone. It has to be a phone that meets or exceeds competitors in both price and performance. Something the surface didn't do.