Interactions between soil quality and climate change may influence the capacity of croplands to produce sufficient food. Here, we address this issue by using a new dataset of soil, climate and associated yield observations for 12,115 site-years representing 90% of total cereal production in China. Across crops and environmental conditions, we show that high-quality soils reduced the sensitivity of crop yield to climate variability leading to both higher mean crop yield (10.3 ± 6.7%) and higher yield stability (decreasing variability by 15.6 ± 14.4%). High-quality soils improve the outcome for yields under climate change by 1.7% (0.5–4.0%), compared to low-quality soils. Climate-driven yield change could result in reductions of national cereal production of 11.4 Mt annually under representative concentration pathway RCP 8.5 by 2080–2099. While this production reduction was exacerbated by 14% due to soil degradation, it can be reduced by 21% through soil improvement. This study emphasizes the vital role of soil quality in agriculture under climate change.