We give a derivation of quantum spectral curve (QSC) — a finite set of Riemann-Hilbert equations for exact spectrum of planar N=4 SYM theory proposed in our recent paper Phys. Rev. Lett. 112 (2014). We also generalize this construction to all local single trace operators of the theory, in contrast to the TBA-like approaches worked out only for a limited class of states. We reveal a rich algebraic and analytic structure of the QSC in terms of a so called Q-system — a finite set of Baxter-like Q-functions. This new point of view on the finite size spectral problem is shown to be completely compatible, though in a far from trivial way, with already known exact equations (analytic Y-system/TBA, or FiNLIE). We use the knowledge of this underlying Q-system to demonstrate how the classical finite gap solutions and the asymptotic Bethe ansatz emerge from our formalism in appropriate limits.