Slavery was the central, destabilizing undercurrent of the conflict, though not the overt casus belli.
Southern Democrats championed territorial acquisition explicitly to create new slave states, balancing Senate power against the free North.
Mexico abolished slavery in 1829. Texas colonists (largely Southern slaveholders) revolted partly to preserve it. Annexation of Texas (1845) as a slave state inflamed Northern opposition.
Rep. David Wilmot (D-PA) proposed banning slavery in any territory acquired from Mexico. Passed House (North vs. South), blocked in Senate. Crystallized the sectional fracture.
U.S. gained California, Nevada, Utah, Arizona, New Mexico. The Missouri Compromise line (36°30′) didn't apply. Status of slavery in these vast lands became the explosive question.
California admitted free; Utah/New Mexico Territories opened to popular sovereignty; Fugitive Slave Act strengthened; slave trade banned in D.C. A temporary patch that delayed but could not prevent Civil War.
Summary: The war was the catalyst that forced the slavery question onto the national stage in a way no compromise could permanently resolve.