The partition function of the square lattice Ising model on the rectangle with open boundary conditions in both directions is calculated exactly for arbitrary system size $Ltimes M$ and temperature. We start with the dimer method of Kasteleyn, McCoy & Wu, construct a highly symmetric block transfer matrix and derive a factorization of the involved determinant, effectively decomposing the free energy of the system into two parts, $F(L,M)=F_mathrm{strip}(L,M)+F_mathrm{strip}^mathrm{res}(L,M)$, where the residual part $F_mathrm{strip}^mathrm{res}(L,M)$ contains the nontrivial finite-$L$ contributions for fixed $M$. It is given by the determinant of a $frac{M}{2}times frac{M}{2}$ matrix and can be mapped onto an effective spin model with $M$ Ising spins and long-range interactions. While $F_mathrm{strip}^mathrm{res}(L,M)$ becomes exponentially small for large $L/M$ or off-critical temperatures, it leads to important finite-size effects such as the critical Casimir force near criticality. The relations to the Casimir potential and the Casimir force are discussed.