When defining what a comedy is, particularly a Shakespearean comedy, while the ability to make us laugh can be a part of it, the term comedy usually defines a "dramatic form, a structure" ("Comedy: An Introduction"). Typically, comedies surround some sort of familial situation, and tension is developed within the family; the tension leads to a point of calamity, and then the tension is resolved. In a comedy, the tension is always resolved through marriage because comedies surround the topic of families, and as a result, "Marriage is celebrated, as is the family as a whole ... as a social occasion" ("Comedy: An Introduction").
Consistent devices can also be found in Shakespeare's comedies that help distinguish comedies from his other plays. For one thing, the action of the play surrounds the topic of love, and it's the lovers who must overcome the play's problems, leading to the resolution. Shakespeare's comedies also always included stock characters, which are character types seen repeatedly within a certain genre. The types of stock characters Shakespeare included in his comedies are the clever wife, the bragging soldier or knight, servants who are known for their cleverness, the heroine's confidant, and especially clowns or fools ("Shakespeare's Plays: Comedy"). Shakespeare's comedies also revolve around society; hence, his comedies also involve certain characters who run contrary to society. At times his comedies seem to have a sad ending underlying the happy ending because these social misfits are "so lost or misguided that they cannot be accommodated or restored to society" ("Shakespeare's Plays: Comedy"). Malvolio is an example of the type of social misfit who cannot be redeemed and simply remains a social outcast.
Twelfth Night certainly does contain all of the elements needed to make it fit in with the rest of Shakespeare's comedies. However, because Malvolio is humiliated and abandons the company swearing, "I'll be revenged on the whole pack of you" (V.i.391), the play ends on a sadder note than would be expected. Even the weddings must be postponed until Malvolio can be found again because Malvolio has the sea captain who rescued Viola in jail, and the sea captain has Viola's feminine clothes. Orsino does not want to carry on with the wedding until Viola is dressed as a woman. The play continues to end on a sad note due to Feste's final song that basically sings of the harshness of reality and how foolishness plays a role in the harshness of life, as we see in the first few lines:
When that I was a little tiny boy,
With hey, ho, the wind and the rain,
A foolish thing was but a toy,
For the rain it raineth every day. (V.i.401-04)
However, regardless of the fact that the play ends on a sadder note than would be expected, the play still surrounds the topics of family, love, and society, plus ends in marriage rather than death or any other great, tragic loss. Therefore, the play most definitely fits the dramatic structure of a comedy rather than any other of Shakespeare's play structures, such as his histories or tragedies.