The gravediggers have several functions. Dramatically, they break the tension, allowing the audience to release some of their pent up emotion before the final emotionally draining scene (the quip about it being ok to be crazy in England because the English won't know it is a direct hit at the audience).
In terms of the play, they are the voice of the common man. They sing because they have been digging graves so long they are accustomed to it and are no longer shocked. It is their work.
Their singing also contrasts with Hamlet's heaviness, as their acceptance contrasts with his introspection.
Finally, they may also function as a commentator on the social conventions of the time. When they speak of Ophelia's proper placement and of the law (did she go to the water or did the water go to her), they are bringing to the audience an awareness that the laws of nature and of God may be different for them (the common man) than for a woman of Ophelia's status for whom the laws may be changed and the suicide magically termed accidental. They accurately identify this as sophistry.
There also seems to be some careful questioning of the state. One of the grave-diggers makes a joke about the gallows being well used, to which the other responds by warning him against claiming that the gallows is stronger than the church,and suggests that anyone who says that risks the gallows themselves. The grave diggers have sneaked in a challenge but because it comes from them, it may pass.