Context. Submillimeter Array (SMA) 870 micron polarization observations of the hot molecular core G31.41+0.31 revealed one of the clearest examples up to date of an hourglass-shaped magnetic field morphology in a high-mass star-forming region. Aims. To better establish the role that the magnetic field plays in the collapse of G31.41+0.31, we carried out Atacama Large Millimeter/submillimeter Array (ALMA) observations of the polarized dust continuum emission at 1.3 mm with an angular resolution four times higher than that of the previous (sub)millimeter observations to achieve an unprecedented image of the magnetic field morphology. Methods. We used ALMA to perform full polarization observations at 233 GHz (Band 6). The resulting synthesized beam is 0.28"x0"20 which, at the distance of the source, corresponds to a spatial resolution of ~875 au. Results. The observations resolve the structure of the magnetic field in G31.41+0.31 and allow us to study the field in detail. The polarized emission in the Main core of G31.41+0.41is successfully fit with a semi-analytical magnetostatic model of a toroid supported by magnetic fields. The best fit model suggests that the magnetic field is well represented by a poloidal field with a possible contribution of a toroidal component of ~10% of the poloidal component, oriented southeast to northwest at ~ -44 deg and with an inclination of ~-45 degr. The magnetic field is oriented perpendicular to the northeast to southwest velocity gradient detected in this core on scales from 1E3-1E4 au. This supports the hypothesis that the velocity gradient is due to rotation and suggests that such a rotation has little effect on the magnetic field. The strength of the magnetic field estimated in the central region of the core with the Davis-Chandrasekhar-Fermi method is ~8-13 mG and implies that the mass-to-flux ratio in this region is slightly supercritical ...