Viola is shipwrecked on the coast of Illyria and she comes ashore with the help of a captain. She loses contact with her twin brother, Sebastian, whom she believes to be dead. Disguising herself as a young man under the name Cesario, she enters the service of Duke Orsino through the help of the sea captain who rescues her. Duke Orsino has convinced himself that he is in love with Olivia, whose father and brother have recently died, and who refuses to see charming things, be in the company of man and entertain love or marriage proposals from any one until seven years have passed, the Duke included. Duke Orsino then uses 'Cesario' as an intermediary to profess his passionate love before Olivia. Olivia however, forgetting about the seven years in his case, falls in love with 'Cesario', as she does not realise 'he' is Viola in disguise. In the meantime, Viola has fallen in love with the Duke Orsino, creating a love triangle between Duke Orsino, Olivia and Viola.
In the comic subplot, several characters conspire to make Olivia's pompous steward, Malvolio, believe that Olivia has fallen for him. This involves Olivia's uncle, Sir Toby Belch; another would-be suitor, a silly squire named Sir Andrew Aguecheek; her servants Maria and Fabian; and her fool, Feste. Sir Toby and Sir Andrew engage themselves in drinking and revelry, thus disturbing the peace of Olivia's house until late into the night, prompting Malvolio to chastise them. Sir Toby famously retorts, "Dost thou think, because thou art virtuous, there shall be no more cakes and ale?" (Act II, Scene III) Sir Toby, Sir Andrew, and Maria are urged to plan revenge on Malvolio. They convince Malvolio that Olivia is secretly in love with him by planting a love letter, written by Maria in Olivia's hand. It asks Malvolio to wear yellow stockings cross-gartered, to be rude to the rest of the servants, and to smile constantly in the presence of Olivia. Malvolio finds the letter and reacts in surprised delight. He starts acting out the contents of the letter to show Olivia his positive response. Olivia is shocked by the changes in Malvolio and leaves him to the contrivances of his tormentors. Pretending that Malvolio is insane, they lock him up in a dark chamber. Feste visits him to mock his insanity, both disguised as a priest and as himself.
Meanwhile, Sebastian (who had been rescued by a sea captain, Antonio) arrives on the scene, which adds confusion of mistaken identity. Mistaking Sebastian for 'Cesario', Olivia asks him to marry her, and they are secretly married in a church. Finally, when 'Cesario' and Sebastian appear in the presence of both Olivia and Orsino, there is more wonder and confusion at their similarity. At this point Viola reveals she is a female and that Sebastian is her twin brother. The play ends in a declaration of marriage between Duke Orsino and Viola, and it is learned that Sir Toby has married Maria. Malvolio swears revenge on his tormentors, but Orsino sends Fabian to console him.