Four times in Genesis 39 we read that God was with Joseph (39:2-3, 21, 23). The statements form a set of pillars at either end of the story of Joseph's initial experience of Egypt. On the one end, they come at the beginning of the story after Joseph has been sold by the Ishmaelites to Potiphar, the pharaoh's "captain of the guard" (39:1). The point of the description is to show to us that God's presence "prospered" Joseph (39:2). He was a "successful man" (39:2) because "the Lord was with him" (39:3). William Tyndale translated it, "the Lord was with Joseph and he was a lucky fellow!" The point is that the presence of God in the life of Joseph prospered him. He was put in charge of Potiphar's entire house entrusting everything that he had to Joseph. God was there, in the good times. True, he was a slave, but life was good.
It is relatively easy to reason that when things are going well that this represents blessings of God. Most of us fall into it by default: things are going well and we thank God for "every good and perfect gift that comes from above." We count our blessings and name them one by one. In the abundance of provision and security of a life where things are going well for us, it is reasonable to conclude that God is in the midst of all of this.
But Moses, in writing the account of Joseph, has a more profound theology than this. As the story develops, things suddenly, and without warning, turn bad. Joseph finds himself the victim of a false accusation of sexual assault—rape, if you will. It is a nightmare scenario where we are told unequivocally that he is utterly innocent. But accuse someone of rape, and some are bound to believe it no matter how loud the protest. Joseph has no recourse to law. "Hell hath no fury like a woman scorned" wrote William Congreve in The Mourning Bride (1697), and Potiphar's wife, a jilted woman to be sure, cries foul, and, understandably, the husband has only one course of action at his disposal: Joseph is imprisoned. The fact he was put in the "King's prison" (39:20), certainly not the worst Egyptian penitentiary, probably indicates that Potiphar may well have doubted his wife's integrity.
What now? When things suddenly turn dark, what are we to think of God's promises to His children? It is one thing to reason that God is with us when things are going well. It is another to conclude the very same thing when things are going badly. And yet, this is precisely what Moses does. Joseph "was there in prison," but "the Lord was with Joseph and showed him steadfast love and gave him favor in the sight of the keeper of the prison. ...the Lord was with him. And whatever he did, the Lord made it succeed." (Gen. 39:20-21, 23). The very same language as before! God was with Joseph in the bad times as well as the good times.
It is worth pondering what this means. We might have expected Joseph to reason that because things had turned worse, God must surely be against him. It is natural for us to assume that bad things are indicative of chastisement. "What have I done to deserve this?" we ask. The false accusations made against Joseph would then be an example of instant retribution. God was punishing him for something he had done. This was exactly the reasoning of Job's friends. They only had one song (Calvin said in a sermon on Job), and they sang it to death! And, because we do believe in divine retribution, this sometimes is the case. Paul seems to be saying as much when he comments on the reason why some of the Corinthians are sick and dying (1 Cor. 11:29-30). But such a conclusion is not a necessary one, and in this instance it would be an entirely false one. Outward suffering is not necessarily an indicator that God is against us.
What Joseph did not know, but what the end of the story in Genesis 50 makes clear, is that God had a purpose in mind in placing him in prison. He would be the right man in the right place when the pharaoh would be losing sleep due to a recurring dream. God would have the interpreter of the dream there at hand in the king's own prison, having exercised his gift among the pharaoh's former butler and baker (who are also in prison). God is weaving a plan, which in its macrocosm will lead to the raising of Joseph to leadership and the rescue of this covenant family from the famine that befalls his homeland. Joseph's imprisonment is part of the unfolding of the greater plan of redemption on the pages of history.
God moves in a mysterious way;
His wonders to perform;
He plants his footsteps in the sea,
And rides upon the storm.
—William Cowper, 1774
Only faith in the "steadfast love" of God towards His own (Gen. 39:21) will reason this way. But it is the way of faith to reason in just this way. No matter how dark the path gets, there is a reason for it. I may not know it; but that is not important. What is important is this: He knows!