Spanning trees on hypercubic lattices and nonorientable surfaces