Ecological restoration projects have significantly altered the environment of China’s Loess Plateau. However, the interaction and relative importance of the underlying natural and socioeconomic drivers remain poorly understood. Thus, this study employed the Integrated Valuation of Ecosystem Services and Tradeoffs (InVEST) model, the Optimal Parameters-based Geographical Detector (OPGD) model, and Structural Equation Modeling (SEM) to assess the evolution of habitat quality (HQ) and its driving factors in the Yanhe River Basin from 1980 to 2020.The results indicate that HQ followed a sigmoidal (S-shaped) trajectory over time and exhibited significant spatial clustering (P < 0.001). It was positively correlated with the expansion of woodland and grassland but strongly negatively correlated with cultivated land and build-up land (P < 0.001). Among the drivers, population density (POP) and gross domestic product (GDP) were key factors exerting strong negative effects (path coefficients of −0.23 and −0.41, respectively), whereas the normalized difference vegetation index (NDVI) and slope showed significant positive effects (path coefficients of 0.17 and 0.23, respectively). Precipitation (PRE) indirectly improved HQ by enhancing NDVI. Furthermore, higher PRE, steeper slope, and higher GDP amplified the positive effect of NDVI, while lower PRE intensified the negative impact of human activities. In summary, ecological restoration projects markedly improved HQ in the basin, underscoring the synergistic roles of natural conditions and socioeconomic factors in shaping environmental outcomes.