This is the number 1 reason why they decided to buy Motorola Mobility. Apple and Microsoft were harassing Android manufacturers using their patents and Google wanted a way to protect Android manufacturers. Patents are like weapons of mass destruction, big companies need them to avoid getting sued for patent infringement. Whenever a company tries to sue them, they simply counter-sue them. Large part of the Motorola Mobility transaction was for patents. Google is going to retain ownership of the majority of patents. Now Apple and Microsoft won't bully them with their nuclear weapons because they also have the nuclear weapons.
To protect Android
Samsung was changing Android and applying their own skins in such a way that normal users weren't gonna recognize that they are using Android, in future they were simply going to release their phones with their own Android clone which they got for close to nothing. Google had take steps to protect Android from Samsung's monopoly. So, Google had to buy Motorola Mobility to bully Samsung into taking a step back. If Samsung didn't back down, Google was simply gonna use power they have over Android to kill Samsung.
To better compete with Apple and Microsoft
They knew that Microsoft is going to buy Nokia and Apple already owns both hardware and software. It is much easier to develop a gadget when you control the software. That's why Apple devices are so well designed and function properly while majority of Android look like cheap knock-offs from the East. It makes things easier when you control both hardware and software.
Research and Development team
Another reason may be that they wanted to acquire the team, instead of building one from scratch. This is what is called an acqui-hire in technology circles. It is an acquisition done solely for to acquire a team. Normally when companies are acquired, they get paid just enough to convince them to dump their idea to conquer the world and join the bigger company with big pockets. Majority of Google products are acquisition. Android was an acquisition. YouTube was an acquisition and a whole lot of other companies that were acquired just for talent.
To avoid anti-trust cases
They also did it to avoid competition commission. Android now have monopoly over the smartphone market. If Motorola became super-successful, it was going to be seen as an abuse of that power. That's why they sold Motorola Mobility with a combination of cash and stock, so that they can still retain some ownership and push Motorola Mobility to the top while avoiding competition commissions at the same time.
They needed to spend money
When a company carries too much cash in hand, investors get worried because that money is supposed to be used to grow the company, or returned back to the investors in the form of dividends.