With society’s emphasis on environmental, social and governance (ESG) factors, how enterprises make effective financing decisions and optimize their capital structure under ESG rating constraints has become a key factor affecting their sustainable development. This paper explores the synergistic optimization of enterprises’ green investment and financing decisions and capital structure under ESG rating constraints through game simulation model and empirical analysis. First, the study constructs a game model to analyze the decision-making behaviors and interactions among the three parties: enterprises, ESG rating agencies and investors, and simulates the strategic choices under different scenarios and their impacts on corporate decisions. Secondly, by empirically analyzing 217 enterprises with the ESG ratings of Shangdao Ronggreen from 2018 to 2023, it is found that the ESG ratings show a significant correlation with the adjustment of capital structure. The results show that for every 1-unit improvement in corporate ESG performance, the speed of capital structure adjustment significantly increases by 0.312 units. In addition, financing constraints and agency costs play a mediating role in the impact of ESG performance on capital structure adjustment. Finally, a capital structure optimization path based on ESG rating constraints is proposed, including innovative financing methods and improving the quality of information disclosure, which aims to help enterprises achieve capital structure optimization and sustainable development in the context of green finance.