ABSTRACT: The development of immunotherapy has greatly changed the advanced-stage non-small-cell lung cancer (NSCLC) treatment landscape. The complexity and heterogeneity of tumor microenvironment (TME) lead to discrepant immunotherapy effects among patients at the same pathologic stages. This study is aimed at exploring potential biomarkers of immunotherapy and accurately predicting the prognosis for advanced NSCLC patients. RNA-seq data and clinical information on stage III/IV NSCLC were obtained from The Cancer Genome Atlas (TCGA) and Gene Expression Omnibus (GEO). In TCGA-NSCLC with stage III/IV (n = 192), immune scores and stromal scores were calculated by using the ESTIMATE algorithms. Univariate, LASSO, and multivariate Cox regression analyses were performed to screen prognostic TME-related genes (TMERGs) and constructed a gene signature risk score model. It was validated in external dataset including GSE41271 (n = 91) and GSE81089 (n = 36). Additionally, a nomogram incorporating TMERG signature risk score and clinical characteristics was established. Further, we accessed the proportion of 22 types of tumor-infiltrating immune cells (TIIC) from the CIBERSORT website and analyzed the difference between two risk groups. OS of patients with high immune/stromal scores were higher (log-rank P = 0.044/log-rank P = 0.048). Multivariate Cox regression identified six prognostic TMERGs, including CD200, CHI3L2, CNTN1, CTSL, FYB1, and SLC52A1. We developed a six-gene risk score model, which was validated as an independent prognostic factor for OS (HR: 3.32, 95% CI: 2.16-5.09). Time-ROC curves showed useful discrimination for TCGA-NSCLC cohort (1-, 2-, and 3-year AUCs were 0.718, 0.761, and 0.750). The predictive robustness was validated in the external dataset. The C-index and 1-, 2-, and 3-year AUCs of nomogram were the largest, which demonstrated the nomogram had the greatest predictive accuracy and effectiveness and could be used for clinical guidance. Besides, the increased infiltration of T cells regulatory (Tregs) and macrophages M2 in the high-risk group suggested that chronic inflammation may reduce survival probability in patients with advanced NSCLC. We conducted a comprehensive analysis of the tumor microenvironment and identified the TMERG signature, which could predict prognosis accurately and provide a reference for the personalized immunotherapy for advanced NSCLC patients.