Purpose. Establishing prognostic gene signature to predict clinical outcomes and guide individualized adjuvant therapy is necessary. Here, we aim to establish the prognostic efficacy of a gene signature that is closely related to tumor immune microenvironment (TIME). Methods and Results. There are 13,035 gene expression profiles from 130 tumor samples of the non-small cell lung cancer (NSCLC) in the data set GSE103584. A 5-gene signature was identified by using univariate survival analysis and Least Absolute Shrinkage and Selection Operator (LASSO) to build risk models. Then, we used the CIBERSORT method to quantify the relative levels of different immune cell types in complex gene expression mixtures. It was found that the ratio of dendritic cells (DCs) activated and mast cells (MCs) resting in the low-risk group was higher than that in the high-risk group, and the difference was statistically significant (P<0.001 and P=0.03). Pathway enrichment results which were obtained by performing Gene Set Variation Analysis (GSVA) showed that the high-risk group identified by the 5-gene signature had metastatic-related gene expression, resulting in lower survival rates. Kaplan–Meier survival results showed that patients of the high-risk group had shorter disease-free survival (DFS) and overall survival (OS) than those of the low-risk group in the training set (P=0.0012 and P<0.001). The sensitivity and specificity of the gene signature were better and more sensitive to prognosis than TNM (tumor/lymph node/metastasis) staging, in spite of being not statistically significant (P=0.154). Furthermore, Kaplan–Meier survival showed that patients of the high-risk group had shorter OS and PFS than those of the low-risk group (P=0.0035, P<0.001, and P<0.001) in the validating set (GSE31210, GSE41271, and TCGA). At last, univariate and multivariate Cox proportional hazard regression analyses were used to evaluate independent prognostic factors associated with survival, and the gene signature, lymphovascular invasion, pleural invasion, chemotherapy, and radiation were employed as covariates. The 5-gene signature was identified as an independent predictor of patient survival in the presence of clinical parameters in univariate and multivariate analyses (P<0.001) (hazard ratio (HR): 3.93, 95% confidence interval CI (2.17–7.1), P=0.001, (HR) 5.18, 95% CI (2.6995–9.945), P<0.001), respectively. Our 5-gene signature was also related to EGFR mutations (P=0.0111), and EGFR mutations were mainly enriched in low-risk group, indicating that EGFR mutations affect the survival rate of patients. Conclusion. The 5-gene signature is a powerful and independent predictor that could predict the prognosis of NSCLC patients. In addition, our gene signature is correlated with TIME parameters, such as DCs activated and MCs resting. Our findings suggest that the 5-gene signature closely related to TIME could predict the prognosis of NSCLC patients and provide some reference for immunotherapy.