The dynamics of two-dimensional natural convection in liquid metal under the influence of magnetic fields within a confined square enclosure are numerically investigated using a high-accuracy method. The study focuses on the flow transition process influenced by both horizontal and vertical magnetic fields, examining the impacts of temperature difference, magnetic field strength, and orientation on flow characteristics and heat transfer. The range of Rayleigh numbers (Ra) explored is from 104 to 106, while Hartmann numbers (Ha) vary from 0 to 100. Liquid gallium is used as the working fluid with a Prandtl number (Pr) of 0.025. Our investigation reveals rich variations in flow patterns, categorized into unsteady periodic, steady “two-in-one” vortex, and steady single vortex states. As Rayleigh numbers increase, a complex series of flow transitions is observed, leading to enhanced flow dynamics. The transition from a steady state to periodic motion is accompanied by a notable increase in heat transfer efficiency. However, increasing the strength of the magnetic field suppresses the flow. A sufficiently strong suppression transitions the flow from periodic motion back to a steady state, resulting in a gradual decrease in heat transfer efficiency. For the parameters considered, the suppression efficiency of horizontal magnetic fields is stronger than that of vertical ones, especially when Ra>6×105 and Ha=100, with a 14% greater suppression efficiency. A scaling law for heat transfer is also identified: Nu:Ra/Ha2γ, where 14≤γ≤12, independent of the magnetic field orientation.