Layered perovskites Sr2IrO4 and Ba2IrO4 are regarded as the key materials for understanding the properties of magnetic relativistic insulators, mediated by the strong spin-orbit (SO) coupling. One of the most fundamental issues is to which extent these properties can be described by the superexchange (SE) model, formulated in the limit of the large Coulomb repulsion. In the present work we address this issue by deriving the relevant models and extracting parameters of these models from the first-principles calculations. First, we construct the effective Hubbard-type model for the t2g bands, by recasting the problem in the language of Wannier orbitals. Then, we map the obtained electron model onto the pseudospin model by applying the theory of SE interactions. We discuss the microscopic origin of anisotropic SE interactions, inherent to the compass Heisenberg model, and the appearance of the antisymmetric Dzyaloshinskii-Moriya term, associated with the additional rotation of the IrO6 octahedra in Sr2IrO4. In order to evaluate the Neel temperature (TN), we employ the non-linear sigma model. While for Sr2IrO4 our value of TN agrees with the experimental one, for Ba2IrO4 it is overestimated by a factor two. We argue that this discrepancy is related to limitations of the SE model: while for more localized t2g states in Sr2IrO4 it works reasonably well, the higher-order terms, beyond the SE model, play a more important role in the more "itinerant" Ba2IrO4, giving rise to the new type of isotropic and anisotropic exchange interactions. This conclusion is supported by unrestricted Hartree-Fock calculations for the same electron model, where in the case of Ba2IrO4, already on the mean-field level, we were able to reproduce the experimentally observed magnetic ground state, while for Sr2IrO4 the main results are essentially the same as in the SE model.