Shakespeare never answered this question directly, but we can easily guess several motivations for him to write his plays.
The first is a simple desire to please other people, to do something remarkable that will attract praise. Shakespeare comes as close as he ever did to a direct statement in the final lines of The Tempest, probably his last full play, where Prospero seems to step partially out of his stage role and begin speaking in a different voice:
Gentle breath of yours my sails
Must fill, or else my project fails,
Which was to please. (The Tempest, epilogue)
Prospero, the character, has not been motivated by a desire "to please" -- this is Shakespeare talking here.
A second motivation was probably a desire to comment indirectly on contemporary events, to offer praise or warnings. Julius Caesar, for instance, is clearly a sermon on how it is permissible to deal with tyranny, and Macbeth both flatters King James and warns him of the results of ambition taken too far. The England of his time was not a democracy, and such indirect commentary gave Shakespeare a much more powerful voice than he otherwise would have had.
But finally, and probably most important, Shakespeare wrote plays to make money. He came from a family of modest means, living away from London, and made himself into a "gentleman," a successful enterpreneur, and a considerable property owner, all through the power of his pen. Thus, despite the genius of his work, we need not doubt that the primary motivation for Shakespeare's plays was a common one -- to put food on the table for himself and his family.