A self-consistent theory of the current-induced switching of magnetization using nonequilibrium Keldysh formalism is developed for a junction of two ferromagnets separated by a nonmagnetic spacer in the ballistic limit. It is shown that the spin-transfer torques responsible for current-induced switching of magnetization can be calculated from first principles in a steady state when the magnetization of the switching magnet is stationary. A steady state is achieved when the spin-transfer torque, proportional to bias voltage in the linear response regime, is balanced by the torque due to anisotropy fields. The spin-transfer torque is expressed in terms of one-electron surface Green functions for the junction cut into two independent parts by a cleavage plane immediately to the left and right of the switching magnet. The surface Green functions are calculated using a tight-binding Hamiltonian with parameters determined from a fit to an ab initio band structure. This treatment yields the spin transfer torques taking into account rigorously contributions from all the parts of the junction. The spin-transfer torque has two components, one with the torque vector T-parallel to, in the plane containing the magnetizations of the two magnetic layers and another with the torque vector T-perpendicular to perpendicular to this plane. It is shown that, in general, T-parallel to and T-parallel to may be comparable in magnitude and they both tend to finite values independent of the spacer thickness in the limit of a thick spacer. T-perpendicular to is shown to be small when the exchange splitting of the majority- and minority-spin bands in both ferromagnets tends to infinity or in the case when the junction has a plane of reflection symmetry at the center of the spacer. The torques T-perpendicular to and T-parallel to are comparable for a Co/Cu/Co(111) junction when the switching Co layer is one or two atomic planes thick. T-perpendicular to is approximate to 27% of T-parallel to, even for a switching Co magnet of ten atomic planes. Depending on material parameters of the junction, the relative sign of T-perpendicular to and T-parallel to can be negative as well as positive. In particular, T-perpendicular to/T-parallel to<0 for Co/Cu/Co(I 11) with switching Co magnet of one atomic plane and T-perpendicular to/T-parallel to > 0 for two atomic planes of Co. A negative sign of the ratio T-perpendicular to/T-parallel to has a profound effect on the nature of switching, particularly in the realistic case of easy-plane (shape) anisotropy much larger than in-plane uniaxial anisotropy. To calculate the hysteresis loops of resistance versus current, and hence to determine the critical current for switching, the microscopically calculated spin-transfer torques are used as an input into the phenomenological Landau-Lifshitz equation with Gilbert damping. In the absence of an applied magnetic field, an ordinary hysteresis loop is the only possible switching scenario when T-perpendicular to/ T-parallel to > 0. However, for T-perpendicular to/T-parallel to < 0, a normal hysteretic switching occurs only at relatively low current densities. When the current exceeds a critical value, there are no stable steady states and the system thus remains permanently in a time dependent state. This is analogous to the observed precession of the switching magnet magnetization caused by a dc current in the presence of an applied magnetic field. The present calculations for Co/Cu/Co(111) show that the critical current for switching in the hysteretic regime is approximate to 10(7) A/cm(2), which is in good agreement with experiment.