Recent research in two-dimensional (2D) materials has boosted a renovated interest in the p–n junction, one of the oldest electrical components which can be used in electronics and optoelectronics. 2D materials offer remarkable flexibility to design novel p–n junction device architectures, not possible with conventional bulk semiconductors. In this Review we thoroughly describe the different 2D p–n junction geometries studied so far, focusing on vertical (out-of-plane) and lateral (in-plane) 2D junctions and on mixed-dimensional junctions. We discuss the assembly methods developed to fabricate 2D p–n junctions making a distinction between top-down and bottom-up approaches. We also revise the literature studying the different applications of these atomically thin p–n junctions in electronic and optoelectronic devices. We discuss experiments on 2D p–n junctions used as current rectifiers, photodetectors, solar cells and light emitting devices. The important electronics and optoelectronics parameters of the discussed devices are listed in a table to facilitate their comparison. We conclude the Review with a critical discussion about the future outlook and challenges of this incipient research field.