Several dramatic purposes are served, though not all of them are made visible immediately.
First and most simply, more characters are on stage. This diffuses the tension between Hamlet and Claudius, and delays direct confrontation.
Second, it reminds us of another larger and happier world, before Claudius.
Third, the themes discussed (playing roles, change, but also the content of the play) relate to Hamlet's larger action.
And fourth, once they are there, foreshadowing.
The players are main characters in the play . They foreshawdows the events that are going to be revealed or rather are revealed. It forewarns the King that someone knows how King Hamlet was killed and that revenge is going to take place in the future.