The porter functions as a bridge between Duncan's murder and its discovery. His dark humor also serves to reinforce the feeling of corruption and foreboding that permeates the play.
On the night of Duncan's murder, Macbeth has come back to his rooms in a frenzy after murdering the king. He insists he hears a loud knocking, but it is unclear whether this is real of in his head, as he is quite agitated.
The porter, as the next scene begins, hears the same loud knocking, confirming for the audience that it is real. He is coming off a night of drinking, and rather than answering the door right away, he speculates as to who it can be. In each of his imaginings, it is a homely, down-to-earth person like himself who is being sent to hell for a seemingly minor crime, highlighting by contrast the true severity of the Macbeths' crime.
Of most interest—because it foreshadows Lady Macbeth's suicide when her own "expectation of plenty" of being queen doesn't work out well—is the porter's following speculation:
Here’s a farmer that hanged himself on the expectation of plenty.
Though he doesn't know it, the porter speaks the metaphoric truth when he likens coming through the castle door to entering the gates of hell: the murder of Duncan has transformed the place into Satan's kingdom, where evil now reigns.
When the porter finally does answer the door, it is Macduff and Lennox who come in. They are looking for the king, who has asked to be awakened early.
The porter provides some comic relief after the rising intensity of the plot the Macbeths have brought to fruition by killing Duncan; however, his antics also reveal the truth about the hellish nature of the castle and show how quickly the king's body is discovered after the murder.