Quantum states with long-lived coherence are essential for quantum computation, simulation and metrology. The nuclear spin states of ultracold molecules prepared in the singlet rovibrational ground state are an excellent candidate for encoding and storing quantum information. However, it is important to understand all sources of decoherence for these qubits, and then eliminate them, in order to reach the longest possible coherence times. Here, we fully characterise the dominant mechanisms for decoherence of a storage qubit in an optically trapped ultracold gas of RbCs molecules using high-resolution Ramsey spectroscopy. Guided by a detailed understanding of the hyperfine structure of the molecule, we tune the magnetic field to where a pair of hyperfine states have the same magnetic moment. These states form a qubit, which is insensitive to variations in magnetic field. Our experiments reveal an unexpected differential tensor light shift between the states, caused by weak mixing of rotational states. We demonstrate how this light shift can be eliminated by setting the angle between the linearly polarised trap light and the applied magnetic field to a magic angle of $\arccos{(1/\sqrt{3})}\approx55^{\circ}$. This leads to a coherence time exceeding 6.9 s (90% confidence level). Our results unlock the potential of ultracold molecules as a platform for quantum computation.