We demonstrate a first-principles method to study magnetotransport in materials by solving the Boltzmann transport equation (BTE) in the presence of an external magnetic field. Our approach employs ab initio electronphonon interactions and takes spin-orbit coupling into account. We apply our method to various semiconductors (Si and GaAs) and two-dimensional (2D) materials (graphene) as representative case studies. The magnetoresistance, Hall mobility, and Hall factor in Si and GaAs are in very good agreement with experiments. In graphene, our method predicts a large magnetoresistance, consistent with experiments. Analysis of the steady-state electron occupations in graphene shows the dominant role of optical phonon scattering and the breaking of the relaxation time approximation. Our paper provides a detailed understanding of the microscopic mechanisms governing magnetotransport coefficients, establishing the BTE in a magnetic field as a broadly applicable first-principles tool to investigate transport in semiconductors and 2D materials.