Shakespeare can be used as an excuse or a distraction. In this clip, it's both. It serves as an excuse to explain the odd behavior of the crew of the Enterprise, who have travelled back in time to Mark Twain's San Franscisco. Ah, they're actors! Putting on a Shakespeare play! What could be more natural? It also serves as a distraction to the comic-relief-providing landlady, who keeps popping in at inopportune moments to demand the rent.
Many of the actors in the Star Trek franchise have performed in Shakespeare elsewhere (Patrick Stewart being the most noted example). I wonder what Data would be like as Puck in a more complete production of the play. Hmmmmm. Well, until that happens, we have his "Wither wander you?" to contemplate:
Links: A Gateway to Star Trek Information at Wikipedia.