Unquestionably, land gained from Mexico in the Mexican War was the fuse which ignited the Civil War, although the dispute had been smoldering for some time. John C. Calhoun referred to the new territories as:
the forbidden fruit; the penalty of eating it would be to subject our institutions to political death.
Ralph Waldo Emerson said of the conflict;
The United States may conquer Mexico; but it will be as the man swallows the arsenic.
The dispute was ignited when David Wilmot, a freshman Congressman introduced a proviso to an appropriations bill which would prohibit slavery in territory taken from Mexico. In a speech before the House, he commented:
God forbid that we should be the means of planting this institution [slavery] upon it.
Common belief had been that the slavery issue had been settled by the Missouri Compromise; in fact President James K. Polk proposed settling the issue by extending the Compromise line into the new territories. Proponents on both sides, however, were unwilling to end it there.
Although Wilmot's proviso never became law, it created new debate on the slavery issue. John C. Calhoun, up in years at this point, offered a counter proposal known as the Calhoun Resolutions. He proposed in the resolutions that Congress had no right to prohibit slavery in the territories, as it was protected by the Fifth Amendment to the Constitution.
The two resolutions polarized the slavery debate. Senator Thomas Hart Benton said that they were like a pair of shears. Separately they could do nothing, but together they could split the union.
I would argue that the main reason the war could not be avoided was the presence of the territories in which the slavery question was not yet decided. If the US had been confined to the states that already existed, the tensions between North and South would not have been nearly as severe as they were. The two sections could simply have coexisted as they had been.
But with the territories, the issue of slavery kept coming up. There had to be decisions about where slavery could and could not exist. This became particularly difficult after the Dred Scott decision. Because of the existence of the territories the issue of slavery could not simply be left as it was. It kept on being a source of tension, making the war inevitable.