The Compromise of 1850 happened because of the Mexican-American War.
Before that war, there had been a balance between the slave states and the free states of the US. The two regions didn't always get along, but they had managed (through things like the Missouri Compromise) to live together well enough. But then the war with Mexico brought much more territory into the US. This forced the North and South to again try to find compromise over how much slavery would be extended out of the regions where it already existed. The Compromise of 1850 was the two regions' attempt at such a compromise.
The immediate impetus for the Compromise of 1850 was the desire of California to come into the Union as a free state. The balance of free and slave states had ensured an equal balance of pro and anti-slavery voices in the Senate. The House of Representatives was already heavily anti-slavery, because the North had a higher population than the South. The admission of California to the Union would upset this balance and in the eyes of many in the South, would eventually lead to legislation severely limiting or even prohibiting slavery. There were sufficient southern votes in the Senate to defeat the Enabling Act to allow California into the Union. The Compromise of 1850 was a means of allowing California to become a state. The consideration for the Southern states was passage of the Fugitive Slave Act which allowed for the return of escaped slaves from any state in the Union
Although it was an attempt at resolving the immediate crisis, the Compromise of 1850 in fact created more problems than it solved, as many in the North were infuriated by the Fugitive Slave Act.