Parshah Vayeshev covers the 1st half of the “Joseph” narrative in Torah — dreams; “coat of many colors;” “sold into slavery;” etc. It includes the story of Yehudah and Tamar.
One Shabbat morning, rather than reading from a chumash, I read directly from the Midrash Rabbah — the most central and important of the numerous midrashic collections.
One comment on the selling of Joseph especially struck me:
“Rabbi Shmu’el b. Nachman [taught]: Jacob’s sons were engaged in selling Joseph; Jacob was taken up with his sackcloth and fasting [mourning for Joseph]; Judah was busy taking a wife; while the Holy One, Blessed be He, was creating the Light of the Meshiach.”
The Meshiach was ultimately to come from the “union” of Yehudah and Tamar. That union itself was the result of the selling of Joseph, in which Yehudah assumed leadership in place of Reuven (the first-born), and began his history of defiance, taking a wife from among the Canaanites, in direct defiance of the wishes of Avraham, Yitzhak and Yakov. Yehudah thereby caused Yakov’s misery and, in further defiance, ended up (in a round-about way) producing (with Tamar) Perez, who was the ancestor of David and the Messianic family-line (Ruth 4:12). All the events that were taking place were therefore for an ultimate good.
Joseph himself affirms this to his brothers years later in Egypt, when he says to them, “It wasn’t you who sent me here [i.e. sold me into slavery], but G-d [i.e. to save your lives].” But the rabbis take it even further – Joseph was sold into slavery in order to set in motion a chain of events that would result in the Meshiach, who would bring an end to the suffering of the entire world.
It’s a very profound teaching: G-d’s Will for our good is active even in events that seem in opposition to G-d’s Will!
“Surely it was for my benefit that I suffered such anguish” (Isaiah 38:17; NIV).
In Hebrew, the verse is even more terse and explicit: הנה לשלום מר לי (lit: “Behold! My bitterness is for my good”).
Even if we only take this on an abstract, theological level, it’s startling and thought-provoking. It means that everything that happens to us — even the bitterest of events — is for a good that’s ultimately known to and by G-d. Accepting this can, at the very least, help us to receive whatever happens to us “stoically.”
But the rabbis weren’t theologians. They weren’t debating abstract principles. They meant for us to see the daily events of our own lives in the same, positive way. They meant for us to be more than stoical. We’re supposed to rejoice in it all, even — perhaps especially — when it’s not what we want.
Paul, too, alludes to this in Romans 8:28 — “…in all things G-d works for the good of those who love Him…” although he’s addressing a more specific set of circumstances, rather than life in general.
Elsewhere, the rabbis also teach that G-d has “two aspects” — the midat Gevurah (or “Din;” G-d’s quality of strictness, judgement, justice) and the midat Hesed (G-d’s quality of kindness and giving). But they ultimately resolve the apparent dichotomy by teaching that G-d’s midat Gevurah or Din is actually an expression of G-d’s midat Hesed, for without limits, laws and consequences, society — in fact, the world itself — could not continue to exist.
Rebbe Nachman of Breslav takes this even further, in his teaching “Azamra,” saying that to see the “good” in a person or thing is to see the G-dliness in it.
The rabbis’ purpose wasn’t credal or theological. It was — in the most appropriate modern term I can think of — for our “cognitive” benefit.
Reb Noson, Rebbe Nachman’s chief disciple, wrote:
“May G-d help you [to] understand the hints contained in everything in the world; may He show you how, thourgh them, you can draw closer to Him each and every day, depending on the individual, the place and the time. Everything that happens in the world, be it in life or death, rising prices or falling prices, poverty or wealth, or any other occurrence or incident that takes place in the world — globally, nationally, locally or to an individual — happens only in order to remind us of G-d specifically through this. He, in His wisdom and mercy, is the Cause of everything. Everything that happens is for our eternal good, in order that through everything we should come to know Him, each and every day. For G-d is indeed working His will at this very moment and will continue to work His will.” 
 Healing Leaves — Prescriptions for Inner Strength, Meaning and Hope from the letters of Reb Noson of Breslav; Yitzchok Leib Bell, compiler; Yaakov Gabel, trans.; Breslov Research Institute © 1999; also available through Simcha Press; p. 8 (an excerpt from letter #47)
“Reb Noson” is Rabbi Noson (Nathan) Sternhartz. He served as Rebbe Nachman’s scribe, compiler and editor. After Rebbe Nachman’s passing, Reb Noson was the leader of the Breslaver Hasidim, but never took the title “rebbe.” The Breslaver have never had another rebbe after Rebbe Nachman. Their leader is called the “manhig” (leader), but Rebbe Nachman’s teachings are still the primary focus.