The purpose of the play within the play is to further illustrate Shakespeare's theme of reality vs. illusion. In fact, the mechanicals' performance of the play shows us just how much reality is actually governed by illusion, or fantasy.
The mechanicals' have made a very ambitious choice to write their own play and perform it before Duke Theseus and Hippolyta in honor of their wedding day. Their choice is particularly ambitious because they are common laborers who are uneducated. Flute is a bellows maker; Quince is a carpenter; Snout is a tinker who mends pots and kettles and such; and Snug is a joiner, which is a particular type of carpenter who specializes in doors, windows, and paneling. As Philostrate points out, they are "[h]ard-handed men that work in Athens here, / Which never labour'd in their minds till now" (V.i.76-77). Since the mechanicals have been so ambitious, we see that the reality is that they are incapable of producing the quality of play that they had actually envisioned producing, showing us that they have actually let their fantasies govern reality.
We especially see the ridiculousness of their ambitions when we see them begin to practice and work out their play. In particular, Bottom is ridiculously ambitious when he sees himself performing every major role of the play, even asking to play both Pyramus and Thisbe as well as the lion. His ridiculousness makes him act like the proverbial ass, as Puck feels obliged to point out. We also see the absurdity of their ambition when we witness the mechanicals coming up with comical solutions to their problems, such as a prologue telling the audience that everything that happens in the play is fictitious so that the ladies are not upset by Pyramus's suicide or frightened by the lion. They also come up with the absurd solution to have an actor play the part of the wall and "hold his fingers thus" to represent the chink in the wall so that Pyramus and Thisbe can whisper through the chink (III.i.63-64). They also have an actor dress up as the man in the moon, but he also, for no known reason, brings on stage with him a bush and his dog. Thus, since the mechanicals do such a poor job of presenting the play, we see that they have let their fantasies, or their illusions, govern their reality.
Hence, we see that the purpose of the play within the play is to further portray the theme of reality vs. fantasy and to show that fantasy governs reality.