What purpose docs Mercutio's speech about Mab, Queen of the Fairies, serve in Romeo and Juliet?
It is mainly to a) make fun of Romeo and b) to serve as character development for Mercutio.
This speech is used to make fun of Romeo because Mercutio is mocking Romeo's way of using long-winded speeches to make a point, which is clear in the Balcony Scene later in the act. Since Romeo was about to begin with his "I had a dream tonight", Mercutio jumps the gun and begins to almost imitate him by taking on the voice of a dreamer, Romeo's voice.
This speech is character development for Mercutio because not only is it showing us that he is a care-free, light-hearted, mocking, and rude person, but it shows us that Mercutio has an odd sort of creativity.
To better understand how this scene would play out, I recommend watching this clip: