It has often been said that this scene was intended for comic relief. It would make a Shakespearean audience or a modern-day audience laugh uproariously, especially if acted by a talented comedian. It was created as a comical monolugue in order to answer one question and to distract the audience in order to keep them from asking themselves other questions, such as "Why was Macduff not sleeping inside the castle?" and "Why didn't Duncan appoint someone to wake him who would be sleeping inside the castle, someone like Banquo for instance?"
The Porter is obviously drunk. This is intended to explain why there has been so much knocking at the gate. The knocking at the gate was intended specifically to force Macbeth to put in an appearance, although he had planned to pretend to have been sound asleep in his chamber with his wife when the mutilated body of King Duncan was discovered. Shakespeare wanted his principal character to be present at the horrible discovery. He also wanted Macuff to be the discoverer. The Porter explains to Macduff that the entire household staff was drunk (and therefore no one else could have opened the gate either). Although this scene is comical and certainly evokes laughter from the audience, it has a serious purpose, which is to explain why Macbeth is present when he doesn't want to be present. He is the host. He couldn't pretend that he didn't hear all that knocking. He had to come down in his nightgown to see what was going on.
But this raises other questions. Why did Lord and Lady Macbeth allow all their servants (and presumably their household guards) to get so blind drunk when they were hosting the King himself? Doesn't this make the Macbeth's look negligent and disrespectful? Doesn't it look especially bad for Lady Macbeth, since she is the chatelain? A significant statement by Banquo was inserted to try to answer this question. This is an important quote:
What, sir, not yet at rest? The King's abed.
He hath been in unusual pleasure, and
Sent forth great largess to your offices. (Act 2, Scene 1)
Macbeth cannot punish his servants because Duncan's "great largess" was what got them all so drunk. Macbeth cannot even punish the drunken Porter.
What is important is the knocking, because it leads to great scenes in which Macduff wakes up everybody in the castle while Macbeth suffers agonies of fear, guilt, and remorse. Everybody appears in nightgowns, so that they look like ghosts. Another important quote is to be extracted from what Macduff shouts when he orders the alarm bell to be rung:
As from your graves rise up and walk like sprites
To countenance this horror.--Ring the bell.
Shakespeare wanted to prepare his audience to see the awakened sleepers as sprites, or ghosts, when they appeared onstage. The audience does not really know who else Macbeth might have killed during the night. When Malcolm and Donalbain appear as "sprites," the audience does not know at first whether they are alive or dead. The King's two sons are the last to arrive, so the audience naturally wonders whether they are going to arrive at all, or whether Macbeth might have recovered his nerve and gone back to murder them in their beds.