Hard scattering factorization with heavy quarks: A General treatment