Detection of weak magnetic fields with nanoscale spatial resolution is an outstanding problem in the biological and physical sciences1, 2, 3, 4, 5. For example, at a distance of 10 nm, the spin of a single electron produces a magnetic field of about 1 T, and the corresponding field from a single proton is a few nanoteslas. A sensor able to detect such magnetic fields with nanometre spatial resolution would enable powerful applications, ranging from the detection of magnetic resonance signals from individual electron or nuclear spins in complex biological molecules5, 6 to readout of classical or quantum bits of information encoded in an electron or nuclear spin memory7. Here we experimentally demonstrate an approach to such nanoscale magnetic sensing, using coherent manipulation of an individual electronic spin qubit associated with a nitrogen-vacancy impurity in diamond at room temperature8. Using an ultra-pure diamond sample, we achieve detection of 3 nT magnetic fields at kilohertz frequencies after 100 s of averaging. In addition, we demonstrate a sensitivity of 0.5 T Hz-1/2 for a diamond nanocrystal with a diameter of 30 nm.