Suppose (X, Ω, g) is a compact Spin(7)-manifold, e.g. a Riemannian 8-manifold with holonomy Spin (7), or a Calabi-Yau 4-fold. Let G be U(m) or SU(m), and P → X be a principal G-bundle. We show that the infinite-dimensional moduli space BP of all connections on P modulo gauge is orientable, in a certain sense. We deduce that the moduli space M Spin(7) P ⊂ BP of irreducible Spin(7)-instanton connections on P modulo gauge, as a manifold or derived manifold, is orientable. This improves theorems of Cao and Leung [9] and Muñoz and Shahbazi [42].If X is a Calabi-Yau 4-fold, the derived moduli stack M of (complexes of) coherent sheaves on X is a −2-shifted symplectic derived stack (M, ω) by Pantev-Toën-Vaquié-Vezzosi [46], and so has a notion of orientation by Borisov-Joyce [7]. We prove that (M, ω) is orientable, by relating algebro-geometric orientations on (M, ω) to differential-geometric orientations on BP for U(m)-bundles P → X, and using orientability of BP .This has applications to defining Donaldson-Thomas type invariants counting semistable coherent sheaves on a Calabi-Yau 4-fold, as in Donaldson and Thomas [15], Cao and Leung [8], and Borisov and Joyce [7].