Soft-tissue sarcomas represent a heterogeneous group of cancer, with more than 50 histological subtypes1,2. The clinical presentation of patients with different subtypes is often atypical, and responses to therapies such as immune checkpoint blockade vary widely3,4. To explain this clinical variability, here we study gene expression profiles in 608 tumours across subtypes of soft-tissue sarcoma. We establish an immune-based classification on the basis of the composition of the tumour microenvironment and identify five distinct phenotypes: immune-low (A and B), immune-high (D and E), and highly vascularized (C) groups. In situ analysis of an independent validation cohort shows that class E was characterized by the presence of tertiary lymphoid structures that contain T cells and follicular dendritic cells and are particularly rich in B cells. B cells are the strongest prognostic factor even in the context of high or low CD8+ T cells and cytotoxic contents. The class-E group demonstrated improved survival and a high response rate to PD1 blockade with pembrolizumab in a phase 2 clinical trial. Together, this work confirms the immune subtypes in patients with soft-tissue sarcoma, and unravels the potential of B-cell-rich tertiary lymphoid structures to guide clinical decision-making and treatments, which could have broader applications in other diseases.