The tumor microenvironment is closely related to the progression and immune escape of tumor cells. Tumor-infiltrating immune cells (TIICs) and immune-related genes (IRGs) are indispensable components of the tumor microenvironment and have been demonstrated to be highly valuable in determining the prognosis of multiple cancers. To elucidate the prognostic value of TIICs and IRGs in gastric cancer, we conducted a comprehensive analysis focusing on the abundances of 22 types of TIICs and differentially expressed IRGs based on a dataset from The Cancer Genome Atlas (TCGA). The results showed that great composition differences in TIICs and immune cell subfractions were associated with survival outcomes in different stages. Additionally, 29 hub genes were characterized from 345 differentially expressed IRGs and found to be significantly associated with survival outcomes. Then, an independent prognostic indicator based on ten IRGs was successfully constructed after multivariate adjustment for some clinical parameters. Further validation revealed that these hub IRGs could reflect the infiltration levels of immune cells. Thus, our results confirmed the clinical significance of TIICs and IRGs in gastric cancer and may establish a foundation for further exploring immune cell and gene targets for personalized treatment.