Clinical studies support the efficacy of programmed cell death 1 (PD-1) targeted therapy in a subset of patients with metastatic gastric cancer (mGC). With the goal of identifying determinants of response, we performed molecular characterization of tissues and circulating tumor DNA (ctDNA) from 61 patients with mGC who were treated with pembrolizumab as salvage treatment in a prospective phase 2 clinical trial. In patients with microsatellite instability-high and Epstein-Barr virus-positive tumors, which are mutually exclusive, dramatic responses to pembrolizumab were observed (overall response rate (ORR) 85.7% in microsatellite instability-high mGC and ORR 100% in Epstein-Barr virus-positive mGC). For the 55 patients for whom programmed death-ligand 1 (PD-L1) combined positive score positivity was available (combined positive score cut-off value ≥1%), ORR was significantly higher in PD-L1(+) gastric cancer when compared to PD-L1(-) tumors (50.0% versus 0.0%, P value <0.001). Changes in ctDNA levels at six weeks post-treatment predicted response and progression-free survival, and decreased ctDNA was associated with improved outcomes. Our findings provide insight into the molecular features associated with response to pembrolizumab in patients with mGC and provide biomarkers potentially relevant for the selection of patients who may derive greater benefit from PD-1 inhibition.