Soil carbon and nitrogen levels are key indicators of soil fertility and are used to assess ecological value and safeguard the environment. Previous studies have focused on the contributions of vegetation, topography, physical and chemical qualities, and meteorology to soil carbon and nitrogen change, but there has been little consideration of landscape and ecological environment types as potential driving forces. The study investigated the horizontal and vertical distribution and influencing factors of total carbon and total nitrogen in soil at 0–20 and 20–50 cm depths in the source region of the Heihe River. A total of 16 influencing factors related to soil, vegetation, landscape, and ecological environment were selected, and their individual and synergistic effects on the distributions of total carbon and total nitrogen in soil were assessed. The results show gradually decreasing average values of soil total carbon and total nitrogen from the surface layer to the bottom layer, with larger values in the southeast part of the sampling region and smaller values in the northwest. Larger values of soil total carbon and total nitrogen at sampling points are distributed in areas with higher clay and silt and lower soil bulk density, pH, and sand. For environmental factors, larger values of soil total carbon and total nitrogen are distributed in areas with higher annual rainfall, net primary productivity, vegetation index, and urban building index, and lower surface moisture, maximum patch index, boundary density, and bare soil index. Among soil factors, soil bulk density and silt are most closely associated with soil total carbon and total nitrogen. Among surface factors, vegetation index, soil erosion, and urban building index have the greatest influence on vertical distribution, and maximum patch index, surface moisture, and net primary productivity have the greatest influence on horizontal distribution. In conclusion, vegetation, landscape, and soil physical properties all have a significant impact on the distribution of soil carbon and nitrogen, suggesting better strategies to improve soil fertility.