CO2-enhanced gas recovery (CO2-EGR) is a promising, environment-friendly technology to produce more natural gas from depleted reservoirs and simultaneously sequester CO2. The subsurface flow in the heterogeneous reservoir is usually different from homogenous one, and the heterogeneity significantly affects the gas recovery. The effects of heterogeneity and the optimization of CO2 injection strategy are the key factors in CO2-EGR. Thus, one of the goals of this paper is to conduct simulations of CO2-EGR in both homogeneous and heterogeneous reservoirs to evaluate the effects of reservoir heterogeneity on CO2-EGR. The second goal is to perform optimization studies to determine optimal CO2 injection time and injection rate for achieving optimal natural gas recovery. The CO2-EGR simulations were conducted in a 3D reservoir model with a 'five-spot' well pattern by using the multi-phase simulator TOUGH2. The results show that the layers with low permeability as well as gravity segregation retard upward migration of CO2 and promote horizontal displacement efficiency. The breakthrough time of CO2 and reservoir space of underexploited natural gas directly affect the gas recovery. The optimal injection time is determined as the depleted stage, and the corresponding injection rate is optimized by using a genetic algorithm (GA) integrated with TOUGH2. The optimization of CO2 injection parameters leads to recovery factors (RFs) reaching 62.83% and 64.75% in the homogeneous and heterogeneous cases while simultaneously obtaining the economic benefit of about 8.67 and 8.95 million USD. This study shows significant economic potential as well as environmental benefits of using CO2-EGR in the depleted gas reservoir by optimizing the CO2 injection parameters. The findings of this work could assist in determining the optimal injection strategy for using CO2-EGR in industrial scale gas reservoirs.