This is certainly a popular plot device, occurring in no fewer than five of Shakespeare's comedies: "Twelfth Night," "As You Like It," "The Two Gentlemen of Verona," "Cymbeline" and "The Merchant of Venice." The historical reason for this is well known, especially since the release of the film "Shakespeare in Love:" women weren't allowed on the Elizabethan stage, so female roles, especially young ones, were played by boys whose voices hadn't yet broken. Therefore a girl disguised as a boy could be very convincingly played by a boy disguised as a girl!
However, Shakespeare uses the device far more than most other dramatists of the period. It is also very useful as a way of developing character and creating intimacy; disguised as boys, his heroines can discard the conventions and say what they really feel. For example, Viola (dressed as a boy, Cesario) in "Twelfth Night" tells Duke Orsino "My father had a daughter loved a man," and describes her feelings for the Duke himself; so the audience knows how she feels even if Orsino doesn't.
However, Shakespeare uses the device far more than most other dramatists of the period. It is also very useful as a way of developing character and creating intimacy; disguised as boys, his heroines can discard the conventions and say what they really feel. For example, Viola (dressed as a boy, Cesario) in "Twelfth Night" tells Duke Orsino "My father had a daughter loved a man," and describes her feelings for the Duke himself; so the audience knows how she feels even if Orsino doesn't.