He didn't write it for a specific event or person. The title "Moonlight Sonata" was coined after his death (1832) by poet/critic Ludwig Rellstab, who likened the first movement to moonlight on Lake Lucerne.
Beethoven's title: "Piano Sonata No. 14 in C♯ minor, Op. 27, No. 2" – part of a pair labeled "Sonata quasi una fantasia" (sonata in the manner of a fantasy), indicating formal freedom.