The origin of polarization in kilonovae and the case of the gravitational-wave counterpart AT 2017gfo