Abstract. The main result of this paper is the existence of Galois representations associated with the mod p (or mod p m ) cohomology of the locally symmetric spaces for GL n over a totally real or CM field, proving conjectures of Ash and others. Following an old suggestion of Clozel, recently realized by Harris-Lan-Taylor-Thorne for characteristic 0 cohomology classes, one realizes the cohomology of the locally symmetric spaces for GL n as a boundary contribution of the cohomology of symplectic or unitary Shimura varieties, so that the key problem is to understand torsion in the cohomology of Shimura varieties.Thus, we prove new results on the p-adic geometry of Shimura varieties (of Hodge type). Namely, the Shimura varieties become perfectoid when passing to the inverse limit over all levels at p, and a new period map towards the flag variety exists on them, called the Hodge-Tate period map. It is roughly analogous to the embedding of the hermitian symmetric domain (which is roughly the inverse limit over all levels of the complex points of the Shimura variety) into its compact dual. The Hodge-Tate period map has several favorable properties, the most important being that it commutes with the Hecke operators away from p (for the trivial action of these Hecke operators on the flag variety), and that automorphic vector bundles come via pullback from the flag variety.