Grain boundaries (GBs) are structural imperfections that typically degrade the performance of materials. Here we show that dislocations and GBs in two-dimensional (2D) metal dichalcogenides MX 2 (M = Mo, W; X = S, Se) can actually improve the material by giving it a qualitatively new physical property: magnetism. The dislocations studied all have a substantial magnetic moment of ~1 Bohr magneton. In contrast, dislocations in other well-studied 2D materials are typically non-magnetic. GBs composed of pentagon-heptagon pairs interact ferromagnetically and transition from semiconductor to half-metal or metal as a function of tilt angle and/or doping level. When the tilt angle exceeds 47° the structural energetics favor square-octagon pairs and the GB becomes an antiferromagnetic semiconductor. These exceptional magnetic properties arise from an interplay of dislocation-induced localized states, doping, and locally unbalanced stoichiometry. Purposeful engineering of topological GBs may be able to convert MX 2 into a promising 2D magnetic semiconductor.