Several times, the story mentions dreams to foreshadow its dark conclusion and suggest that sin lurks deep in the psyche of all humankind.
The story's first mention of dreams comes in its second paragraph, when Faith begs Goodman Brown not to leave her for the night, not out of concern for what he might be doing (which is, in fact, sinister), but out of concern about what she might dream while she's alone. "A lone woman is troubled with such dreams and such thoughts, that she's afeard of herself, sometimes," she says. While Faith seems to be worried that she might have a nightmare without her husband there to comfort her, Goodman Brown interprets her comment differently: "Methought, as she spoke, there was trouble in her face, as if a dream had warned her what work is to be done tonight." In other words, he thinks she might be subtly warning him against his journey because she knows, from a dream, that he's going into the woods to meet the devil. This interpretation of Faith's comment is a stretch, but it shows how guilty Goodman Brown's conscience is and—crucially—it reveals his belief that dreams can tell the truth.
Of course, Goodman Brown does go to the woods that night and has a terrifying experience of seeing upstanding townspeople consorting with the devil, including Faith herself. But when he comes back to town the next morning, it's not clear what actually happened—Faith seems unchanged, and her ribbons (symbols of innocence) are still in her hair, even though he saw them fall off in the forest. The story explicitly raises the possibility that Goodman Brown had simply "fallen asleep in the forest, and only dreamed a wild dream of a witch-meeting." In this way, Faith's comment might have literally foreshadowed the story's climax, in which Goodman Brown has a terrifying dream that changes his life.
Crucially, it doesn't matter to Goodman Brown whether what he saw in the forest was a dream or reality—he himself doesn't know, but he still can't let go of what he thought he saw, and it poisons the rest of his life. He cannot find joy or comfort in his marriage, community, or religious faith, because he suspects (based possibly on a dream) that it's all corrupt. This validates Faith's initial concern about the power and danger of dreams. If Goodman Brown believes that dreams can tell the truth (as he implied at the beginning of the story when he fretted that Faith might have been told of his sinful journey in a dream), then it stands to reason that Goodman Brown might find a dream in which Salem's elite are consorting with the devil just as disturbing as seeing it in reality. Even if what he experienced was a dream, he still might find it credible evidence of humankind's irredeemable corruption. And the fact that both Goodman Brown and Faith seem afraid of what their dreams might reveal shows their fear (which is also the story's fear) that sin and evil lie at the center of the human psyche, expressed honestly in dreams.
In the passage below, Goodman Brown's fear of someone sinister lurking in the trees foreshadows a moment later on in the story when he himself hides in the trees, becoming the sinister presence he initially feared:
[...] the traveller knows not who may be concealed by the innumerable trunks and the thick boughs overhead; so that, with lonely footsteps, he may yet be passing through an unseen multitude.
"There may be a devilish Indian behind every tree," said Goodman Brown to himself; and he glanced fearfully behind him, as he added, "What if the devil himself should be at my very elbow!"
Goodman Brown's fear of people lurking in the trees reveals a lot about his moral intuitions. For one, the people he worries about are all outsiders to the community of Salem: unseen strangers, a "devilish Indian," or even the devil himself. It's clear that Goodman Brown assumes that the people of Salem are just as virtuous as they seem, so the only evildoers in the forest must be total strangers. This is an assumption that's quickly proved wrong, as Salem's most pious and respectable citizens all come to consort with the devil, showing that sin and evil live much closer to home than Goodman Brown once thought.
Furthermore, Goodman Brown's initial assumption is that the dangerous people lurking in the trees are other people—not Goodman Brown himself. But this moment foreshadows a thematic turn in the story, in which Goodman Brown becomes the dangerous sinner lurking in the trees. Later in the story, after making the decision to turn back and not follow the devil, Goodman Brown hears horses coming up the path, which makes him "conceal himself within the verge of the forest" so that he's not seen doing something sinful. The people on horseback turn out to be his minister and Deacon Gookin, two Christians he profoundly respects, who are implied to be discussing their excitement over tonight's devil worship. Goodman Brown can't see it in the moment, but he has become the sinful unseen presence in the trees that he once feared, showing that evil and sin don't come from the outside—evil and sin are within the community of Salem and within Goodman Brown himself.