No one can speak for an author, so I can't really tell you why Shakespeare might have created Ros. and Guil. I can tell you the functions or effects the two characters serve.
Ros. and Guil. function in the following ways:
- They provide spies for Claudius, to further the plot and themes, such as appearance and reality.
- They provide characters that betray Hamlet. Their doing Claudius's bidding is not so innocuous as might first seem. If it's no big deal, then why are they so reluctant to admit to Hamlet that they were sent for? They were pegged to be spies by Claudius from the beginning.
- They provide a sounding bound for Hamlet. Hamlet can't deliver everything by soliloquy. They provide someone for Hamlet to tell that he is not really insane, but is only pretending, for instance.
- Hamlet's interactions with them provide characterisation of Hamlet. For instance, Hamlet's hubris and sensitivity concerning someone thinking he/she is smarter than him and can "play" him is revealed in the pipe/play me scene.
Ros. and Guil. are foolish, gullible, interchangeable, and naive. They provide puppets for Claudius, another example of Hamlet being betrayed, a sounding board, and reveal characterization of Hamlet.
These two friends of Hamlet's become pawns of the usurper King Claudius, helping to create the major conflict between King and Prince. They're also pretty humorous.
When Hamlet sees his two old friends for the first time, they're delighted to see one another. Soon after, though, they are commissioned by Claudius to perform an innocent, innocuous task which is probably best done by friends--find out what's bothering Hamlet. They quickly agree, happy to be of service to the King, which is exactly why Claudius chose them for the task.
Unfortunately for them, both Hamlet and Claudius understand that these two aren't particularly bright but are, to some degree, awed in the face of power. Hamlet sees immediately why they are so concerned about him, and Claudius sees them as expendable enough to accompany Hamlet to England.
In the end, Hamlet tells his mother he will trust them as he would "adders fang'd" and has no guilt about signing their death warrant, as it were, when they arrive in England. "They are not near my conscience," he says. The audience sees this as a form of poetic justice--that's what you get when you scheme with the devil, so to speak.
Despite these intricacies of plotting, they are also a bit comical in a tragedy with minimal comic elements. Their constant togetherness is amusing (they are often played as twins, which may be why Claudius confuses the two); their obsequiousness to Claudius is humorous; and their rather startled disbelief that Hamlet would doubt their innocent motives is kind of funny--not to mention those names. There's just something humorous about the way they trip off the tongue, though they were apparently recognizable (even royal) names in Denmark.
These two characters serve both to escalate the plot and add a bit of comic relief.