I was told by someone who had absolutely no authority on the matter that Beethoven composed the first two movements of the Moonlight Sonata as a gift for a female student he was deeply in love with, and thus it's relative simplicity despite being an obviously beautiful song. When she rebuffed him he then composed the final movement to be intentionally impossible for her to play just to spite her.
Is there any truth to this?
Certainly not, since he had never planned to dedicate the sonata to her, much less compose it for her. The "her" in question is Countess Julia Guicciardi, a teenage girl with whom Beethoven was almost certainly in love. The timeline also makes it impossible.
Let's deal with the dedication issue first. Beethoven originally dedicated his Rondo in G, op. 51 No. 2 to her until he realized he had to dedicate something to Countess Henriette Lichnowsky. He asked for the rondo back and dedicated the sonata to her instead. I couldn't find information on whether he had the sonata already completed to dedicate to her, but the second fact makes it clear that he didn't write the third movement out of spite.
The sonata, op. 27, no. 2, was written in 1801 and published in 1802 by Cappi & Diabelli. Beethoven continued to teach with the Guicciardi family until May or June 1803. Thayer, in his Life of Beethoven, concludes that he left on good terms and simply tired of teaching, something he was never a fan of. Thayer is also of the opinion that Beethoven asked for Julia's hand in marriage. Julia and one of her parents were ok with it, but one parent (Thayer thinks is was the father) didn't want their daughter to jeopardize the family by marrying someone not of the aristocratic class, especially such a weird musician who was beginning to go deaf; six months later, she married another man.
So there you have it. He was in love with his student, but she likely didn't turn him down, and he left on pretty good terms (especially for such a grumpy guy). The simple fact is that there's a lot of bullshit mythology around Beethoven because he's the archetype of the artist who won't compromise and makes his art only for himself. A sonata that captures moonlight itself was really just another piece he wrote, a piece that he thought was more popular that it deserved to be.
source:
Thayer's Life of Beethoven, revised and edited by Elliot Forbes
In supplement to u/XRotNRollX answer, u/erus explains the history of the song in some depth here.