This study aimed to investigate the key genes and immune microenvironment involved in different TNM stages of lung adenocarcinoma (LUAD) and lung squamous cell carcinoma (LUSC). The gene expression and clinical characteristics data were downloaded from the genomic data commons (GDC) database. After initial data processing, the characteristics of the immune microenvironment were analyzed. The differentially expressed genes (DEGs) in tumor vs. normal, and in early vs. advanced stages were screened, followed by Spearman correlation test for tumor infiltrating immune cells (TIICs) to identify immune-related genes. Finally, functional enrichment, protein-protein interaction, and survival analyses were performed. In LUAD, early stage was with higher immune scores, greater number of memory B cells and M0 macrophages compared to advanced stage. M0 and M2 macrophages, and resting memory CD4+ T cells accounted for a large proportion of TIICs in LUAD. The abundance of M0 macrophage infiltration was significantly correlated with the TNM stage and survival. In LUSC, early stage was with higher cytolytic activity and neoantigen burden compared to advanced stage. M0 and M2 macrophages, and plasma cells accounted for a large proportion of TIICs in LUSC. The abundance of resting and activated mast cells was significantly correlated with TNM stage, while resting dendritic cells, eosinophils, activated memory CD4 T cells, and mast cells were significantly correlated with prognosis. Tumor mutation burden analysis revealed that the median of variants per sample decreased from stage I to IV in LUAD, while it increased in LUSC. Further, 83 and 9 immune-related DEGs were identified in LUAD and LUSC, respectively, of which 23 genes in LUAD and 2 genes in LUSC correlated with survival. In conclusion, we identified the key genes, and characterized the tumor immune microenvironment in LUAD and LUSC which may provide therapeutic targets for the treatment of NSCLC.