David descends into a state of depression, "haunted by the ghosts of many hopes, of many dear remembrances, many errors, many unavailing sorrows and regrets." Before he has fully realized how depressed he is, he goes abroad, so he has no friends around him when, "little by little," he comes to appreciate the depth of his despair. His grief encompasses Dora, Steerforth, and Ham, but also the loss of the Peggotty's home, and the ruin of "the whole airy castle" of his own life. David travels from place to place but can't imagine his grief ever lifting; in fact, at his lowest points, he expects to die.
David's despair in the months after the emigrants' departure is perhaps the most serious threat to his growth as a person that he has faced. This is because David's depression is completely incompatible with the values he has been learning to embody as a nineteenth-century man; he's living entirely in the past, and can't even imagine a future for himself, let alone take steps to make it a reality. With that said, it's not surprising that David has lost confidence in his ability to build a better life for himself, because he's lost so much of what he worked to achieve—most notably, marriage to Dora. The fact that this relationship had already failed to meet his expectations likely compounds David's despair, because marrying and raising a family were seen as so central to a fulfilling life; implicitly, David's talk about "the whole airy castle of [his] life" involves questioning the meaningfulness of the goals he's been taught to pursue (a career, a family, and so on).
David says he is unable to remember or describe all the forms his grief took during this period; instead, he sees himself wandering through foreign cities "as a dreamer might," without truly taking in what he was seeing. This went on for several months, until he found himself crossing from Italy into Switzerland.
David's aimless travels parallel his mental state at the time: he has no clear idea of where he is going, or any hope of getting there. Most of all, he loses sense of himself as an active participant in his own life. This state of mind is so wildly different from the one David normally inhabits that he has difficulty even remembering or explaining it later. Interestingly, David describes his mindset as "undisciplined"—a word he has previously associated with immaturity. In other words, David depicts his depression as a kind of reversion to a childlike way of thinking and acting.
One evening, David is traveling in the Alps, when he is struck by the "beauty and tranquility" of the valley he is descending into. He feels a sense of "sorrow that was not all oppressive," and begins to think that he might be able to change for the better. When he reaches the town he is staying in, the beauty of his surroundings impresses him even more—particularly the sound of shepherd's singing, which seems to come from the sky and mountains. David lies down in the grass and cries as he hasn't since Dora's death.
The beauty of the Alps doesn't put a stop to David's grief, but it does allow him to experience a kind of cathartic grief that allows for the possibility of "better change." This is why the moment is a turning point in David's journey: although he is still unhappy afterwards, he is at least able to once again look towards the future.
While he is sitting in the grass, David takes out a letter that was waiting in town for him. Opening it, he finds it is from Agnes, and mostly expresses concern for his well-being: Agnes says that she is "happy and useful," but then goes on to express her faith that David will find a way to "turn affliction to good" and emerge with a "firmer and higher tendency." She doesn't give him any advice, but she assures him that she is with him in spirit. David puts the letter away, feeling the "shadows" leaving him and conscious of a deep love for Agnes that he can't put a name to. He reads the letter several times that evening and then writes to her, saying that she inspires him to be what she thinks he already is.
Agnes's letter encapsulates the role she plays in David's life. Although she consistently encourages him to be a better and more mature person, she does so without infringing on his own agency. Here, she simply assures David of her confidence in him, which in turn motivates him to be the wise, self-assured, and resilient person she knows that he can be. Her remark about "turning affliction to good" is especially significant, because it speaks to one of the primary ways in which Dickens suggests people can become masters of their fate rather than victims of it; by reframing his losses as opportunities for growth, David demonstrates his purposefulness and resolve.
It has now been nearly a year since Dora's death. David decides to spend the remaining three months in the same valley and then reassess his situation. Once the three months have passed, he decides not to return home immediately, but to begin writing again. He also begins seeking out human contact again, and makes several friends in the valley before leaving for the winter.
Although David doesn't feel ready to return to England yet, he's beginning to take steps toward creating a future for himself—specifically, by resuming his career as a writer.
David begins working on a story based on his own life. When he finishes, he sends it to Traddles, who arranges for it to be published. The work enhances David's fame, and he soon begins working on another novel. Before he finishes this, however, he begins to think about returning home, now that he has recovered his health and seen many countries.
In using his memories as material for a novel, David follows Agnes's advice about "turning affliction to good" in a very concrete way. He also demonstrates his mastery over his past experiences both by repurposing them as fiction and by using them to further his career.
David explains that he has largely said all he wishes to about this period of his life, but that there is one thing he has held back. He then admits that at some point he began to feel that he had "thrown away" Agnes's love. It's possible, he says, that this feeling was at the heart of his longstanding dissatisfaction with his life, but it intensified during his depression. In fact, this was one thing that persuaded him to stay abroad for so long: he feared that he would rashly confess his love and lose Agnes's "sisterly affection." Furthermore, although he initially hoped that he might be able to win Agnes back at some point in the future, he slowly came to see this as impossible, since he was the one who set the terms of their current, platonic relationship by marrying Dora.
One way in which David turns this painful period of his life to good purpose is by using it as an opportunity for self-reflection. Now that he has time to look back on his life, it becomes clear to him that he mistook his youthful infatuation with Dora for lasting love, and that Agnes is the woman he ought to have married. The fact that she gracefully adopted the sisterly role that David pushed her into simply confirms this, because she selflessly set aside her own feelings to suit his needs.
Before returning to England, David therefore decides to keep his love for Agnes a secret. Doing so causes him pain, but he decides to use the loss of "what might have been" into another means of self-improvement: since it is a reminder of his youthful thoughtlessness, it will cause him to be "more resolved" and "more conscious of [him]self" in the future.
David's decision to remain silent about his feelings stems not only from his belief that Agnes views him as a brother, but also from his sense of being unworthy of her; to David, the fact that did not see his relationship with Agnes for what it was when he married Dora is proof of his foolishness and immaturity. However, while he renounces the possibility of being Agnes's husband, he still looks to her as a guiding light. This is evidence of David's growing maturity, as well as of David and Agnes's suitability for one another; David now has enough self-awareness and self-discipline to recognize Agnes's moral superiority, and to put her well-being first.