We describe an ab initio method for calculating the electronic structure, electronic transport, and forces acting on the atoms, for atomic scale systems connected to semi-infinite electrodes and with an applied voltage bias. Our method is based on the density functional theory (DFT) as implemented in the well tested Siesta approach (which uses non-local norm-conserving pseudopotentials to describe the effect of the core electrons, and linear combination of finite-range numerical atomic orbitals to describe the valence states). We fully deal with the atomistic structure of the whole system, treating both the contact and the electrodes on the same footing. The effect of the finite bias (including selfconsistency and the solution of the electrostatic problem) is taken into account using nonequilibrium Green's functions. We relate the nonequilibrium Green's function expressions to the more transparent scheme involving the scattering states. As an illustration, the method is applied to three systems where we are able to compare our results to earlier ab initio DFT calculations or experiments, and we point out differences between this method and existing schemes. The systems considered are: (1) single atom carbon wires connected to aluminum electrodes with extended or finite cross section, (2) single atom gold wires, and finally (3) large carbon nanotube systems with point defects.