The sub-plot of Twelfth Night, the gulling of Malvolio by Sir Toby Belch, Maria, Feste, and Aguecheek, is justly famous as one of Shakespeare's funniest experiments in New Comedy, that is, in a style of comedy which is basically quite different from the pastoral romantic style of the main plot. The basis for the sub-plot is one of the oldest and most popular subjects for New Comedy - the unmasking of the hypocrite, a satiric exposure of apparent virtue so as to humiliate the hypocrite and make him ridiculous.
The duping of Malvolio is linked to main plot thematically in the obvious sense that it deals with a variety of love, namely, self-love, a wholesale preoccupation with self-interest and a refusal to see anyone as important other than oneself. Such a preoccupation leads to a misconception of the world and a total vulnerability to being manipulated into betraying oneself, as Malvolio does, by trusting that one's desires match the reality of the situation. Malvolio is punished—and is relatively easy to punish—because he is so wrapped up in his own importance that he sees no value in anything else or anyone other than himself. His conceit about himself, along with his secret desires for social advancement and power, make him easy to tempt into ridiculous behaviour.
This point is made most obviously by the instant antipathy between Feste, the fool, and Malvolio. Malvolio sees no point in having a Fool around, especially one who seems as old and tired as Feste, in whose jokes Malvolio finds no amusement. It's important to note that the major motivation for the trick on Malvolio is the insult he makes to the Fool when we first meet them, together with his total dislike for any sort of fun.
Malvolio, in other words, is a kill-joy, a person with no sense of humour and with no place in his scheme of things for anything other than what he thinks is important. Everyone recognizes this. Olivia tells him he is sick of self love, and Sir Toby Belch roars at him some of the most famous lines of the play: "Dost thou think, because thou art virtuous, there shall be no more cakes and ale?". This quality in Malvolio makes him, rather like Jaques in As You Like It, the character most at odds with the comic spirit of the play.
But there's an essential difference between Jaques and Malvolio which makes the latter's presence in the play a good deal heavier. Malvolio is Olivia's steward, the person chiefly responsible for running her household, the master of the accounts. Olivia tells us quite clearly that Malvolio is essential to her—"I would not have him miscarry for the half of my dowry". He may be Olivia's servant, not of the same class as Sir Toby or Sir Andrew, and he may be a hypocrite with thoughts well above his station, but his work carries a weight that clearly matters. And that makes some difference to his final words, in which he promises revenge on all those present.
Three times in one scene (Act II Sc 3), other characters call Malvolio a Puritan, using that term in a derogatory sense to indicate someone they hate, someone who needs to be exposed for what he is. This does not necessarily mean that Malvolio is a radically religious Protestant, but it suggests that what they don't like about him is his excessive devotion to those things the Puritans were known for: seriousness, work, enforcing a strict code of morality in which there was no room for fun, colour, and entertainment (the Puritans were the moving force behind those who wanted to close the theatres as immoral places), and a hostility to art generally. In that sense, the Puritan often becomes (as here in this play) the symbol for an attitude excessively hostile to certain aspects of human experience. Exposing Malvolio thus becomes a way of neutralizing his power as a kill joy.
Malvolio is, of course, successfully humiliated and exposed—the trick is very funny (and helps Shakespeare to put into the play his crudest joke) and the punishment in the prison a damning parody of Puritan doctrine. But one wonders about that promise of revenge. If Malvolio is, as Olivia tells us, essential to the running of her estates, the one who does the major work of keeping the place going (and no one else seems interested or capable of doing that), then his departure at the end of the play casts a certain ironic shadow over the communal joy.