Scalable quantum computation and communication require error control to protect quantum information against unavoidable noise. Quantum error correction protects information stored in two-level quantum systems (qubits) by rectifying errors with operations conditioned on the measurement outcomes. Error-correction protocols have been implemented in nuclear magnetic resonance experiments, but the inherent limitations of this technique prevent its application to quantum information processing. Here we experimentally demonstrate quantum error correction using three beryllium atomic-ion qubits confined to a linear, multi-zone trap. An encoded one-qubit state is protected against spin-flip errors by means of a three-qubit quantum error-correcting code. A primary ion qubit is prepared in an initial state, which is then encoded into an entangled state of three physical qubits (the primary and two ancilla qubits). Errors are induced simultaneously in all qubits at various rates. The encoded state is decoded back to the primary ion one-qubit state, making error information available on the ancilla ions, which are separated from the primary ion and measured. Finally, the primary qubit state is corrected on the basis of the ancillae measurement outcome. We verify error correction by comparing the corrected final state to the uncorrected state and to the initial state. In principle, the approach enables a quantum state to be maintained by means of repeated error correction, an important step towards scalable fault-tolerant quantum computation using trapped ions.