The last section of Genesis focuses on the family of Jacob, primarily the interactions between his favorite son, Joseph, and Joseph's 10 jealous brothers.
In chapter 37 we see the seeds sown of what is coming through Jacob's clear favoritism, Joseph's "tattling" on his brothers to their father, and finally Joseph's foolish decision to tell his brothers about not one but two dreams in which they bowed down to him.
With anger at Jacob's favoritism, and jealousy stoked by Joseph's apparent arrogance (or at best cluelessness), the brothers' desire to do something about it simmers until opportunity arrives. That opportunity is the arrival of Joseph, alone, to visit them.
The hearts of Joseph's brothers were set to murder, but providential grace gave them an opportunity to choose a lesser evil by selling him into slavery.