The possibility that non-magnetic materials such as carbon could exhibit a novel type of s-p electron magnetism has attracted much attention over the years, not least because such magnetic order is predicted to be stable at high temperatures. It has been demonstrated that atomic-scale structural defects of graphene can host unpaired spins, but it remains unclear under what conditions long-range magnetic order can emerge from such defect-bound magnetic moments. Here we propose that, in contrast to random defect distributions, atomic-scale engineering of graphene edges with specific crystallographic orientation--comprising edge atoms from only one sub-lattice of the bipartite graphene lattice--can give rise to a robust magnetic order. We use a nanofabrication technique based on scanning tunnelling microscopy to define graphene nanoribbons with nanometre precision and well-defined crystallographic edge orientations. Although so-called 'armchair' ribbons display quantum confinement gaps, ribbons with the 'zigzag' edge structure that are narrower than 7 nanometres exhibit an electronic bandgap of about 0.2-0.3 electronvolts, which can be identified as a signature of interaction-induced spin ordering along their edges. Moreover, upon increasing the ribbon width, a semiconductor-to-metal transition is revealed, indicating the switching of the magnetic coupling between opposite ribbon edges from the antiferromagnetic to the ferromagnetic configuration. We found that the magnetic order on graphene edges of controlled zigzag orientation can be stable even at room temperature, raising hopes of graphene-based spintronic devices operating under ambient conditions.