The play and its occasion
Written around 1601–02 for the Lord Chamberlain's Men. The title names Twelfth Night, the climax of the Christmas revels and a Feast of Fools presided over by a “Lord of Misrule”, when social order was deliberately inverted. The subtitle, What You Will, signals that holiday spirit of licence.
Boy actors
Every female part was played by a boy, so the first “Cesario” was a boy playing a woman playing a boy — a layered joke the original audience saw fully, and one that keeps the play's questions about gender live.
Puritanism and the theatres
Malvolio (the name suggests “ill will”) is “a kind of Puritan”. The play was written as Puritan hostility to playhouses was hardening (they would close the theatres in 1642), which sharpens his humiliation and his parting line, “I'll be revenged on the whole pack of you”.
Sources
An Italian comedy, Gl'Ingannati (“The Deceived”), reached Shakespeare through Barnabe Riche's tale of Apolonius and Silla. Twins and shipwrecks were stock devices he had already used in The Comedy of Errors.
A comedy with a shadow
Critics often read the play's melancholy against the deaths in Shakespeare's own life and a court entertainment staged in the last years of an ageing, heirless Queen Elizabeth.