By combining neutron four-circle diffraction and polarized neutron-diffraction techniques we have determined the complex spin structures of a multiferroic YMn2O5 that exhibits two ferroelectric phases at low temperatures. The obtained magnetic structure has spiral components in both the low-temperature ferroelectric phases that are magnetically commensurate and incommensurate, respectively. Among proposed microscopic theories for the magnetoelectric coupling, our results are consistent with both the spin-current mechanism and the magnetostriction mechanism. Our results also explain why the electric polarization changes at the low-temperature commensurate-to-incommensurate phase transition