Engineering desired operations on qubits subjected to the deleterious effects of their environment is a critical task in quantum information processing, quantum simulation and sensing. The most common approach relies on open-loop quantum control techniques, including optimal-control algorithms based on analytical or numerical solutions, Lyapunov design and Hamiltonian engineering. An alternative strategy, inspired by the success of classical control, is feedback control. Because of the complications introduced by quantum measurement, closed-loop control is less pervasive in the quantum setting and, with exceptions, its experimental implementations have been mainly limited to quantum optics experiments. Here we implement a feedback-control algorithm using a solid-state spin qubit system associated with the nitrogen vacancy centre in diamond, using coherent feedback to overcome the limitations of measurement-based feedback, and show that it can protect the qubit against intrinsic dephasing noise for milliseconds. In coherent feedback, the quantum system is connected to an auxiliary quantum controller (ancilla) that acquires information about the output state of the system (by an entangling operation) and performs an appropriate feedback action (by a conditional gate). In contrast to open-loop dynamical decoupling techniques, feedback control can protect the qubit even against Markovian noise and for an arbitrary period of time (limited only by the coherence time of the ancilla), while allowing gate operations. It is thus more closely related to quantum error-correction schemes, although these require larger and increasing qubit overheads. Increasing the number of fresh ancillas enables protection beyond their coherence time. We further evaluate the robustness of the feedback protocol, which could be applied to quantum computation and sensing, by exploring a trade-off between information gain and decoherence protection, as measurement of the ancilla-qubit correlation after the feedback algorithm voids the protection, even if the rest of the dynamics is unchanged.