Why did Shakespeare write his sonnets?
We have no evidence of why William Shakespeare wrote his sonnets. He did not leave behind any account of his reasons for writing them. Because he wrote before the advent of copyright, he would not have earned money for writing or publishing the sonnets. Although we can make guesses about his motives, that is all they would be, guesses. Since his motives in writing the sonnets really have no impact on our enjoyment of reading them (basing a reading on authorial intent is known among literary critics as the "intentional fallacy"), the only reason to think about his motives is out of general curiosity.
What we do know is that some of the sonnets were privately circulated before being published. Often writers produced works on specific themes to attract the attention of or to flatter potential patrons.
The beginning of Sonnet 55 suggests that Shakespeare saw writing poems as a path to immortal fame; he states
Not marble, nor the gilded monuments
Of princes, shall outlive this powerful rhyme;
During Shakespeare's period love poems were also a part of relationships, often used as a tool of flirtation or seduction, something that might also have been a motive.