It seems to me that the confusion comes from a misinterpretation of the math.SE post. Let $G$ be a Lie group and $M$ be a manifold. According to the math.SE post, the bundles of structure group $G$ over $M$ are classified by $H^1(M,G)$. But these bundles are different from the $G$-principal bundles relevant in gauge theory. Indeed, for bundles with structure group $G$, the transition functions are constant function equal to some element of $G$ whereas for a $G$-principal bundle, the transition functions are general smooth functions with values in $G$. These two notions are very different in general (they are the same if $G$ is discrete because a function to a discrete thing is automatically constant) and the terminology can be confusing. It is possible to classify $G$-principal bundles along the lines of the math.SE.post but the answer is no longer $H^1(M,G)$ but $H^1(M, C^\infty (M,G))$ where $C^\infty (M,G)$ is the sheaf of smooth functions on $M$ with values in $G$. As mentionned in the comments to the question, we have $H^1(M, C^\infty (M,G)) = [M, BG]$ where $[M, BG]$ denotes the set of homotopy classes of maps from $M$ to $BG$ where $BG$ is a space (well-defined up to homotopy equivalence), called the classifying space of $G$, which has the property to have a $G$-principal bundle whose total space is contractible. In particular, we have the relation between homotopy groups $\pi_i(BG)=\pi_{i-1}(G)$ for all $i$.
The existence of a precise relation between $[M, BG]$ and the cohomology of $M$ depends on the precise $G$ considered.
If $G=U(1)$, then $H^1(M, C^\infty (M,G)) = [M, BG]=H^2(M, \mathbb{Z})$ and so is exactly a cohomology group of $M$. There are many ways to prove that (from the sheaf point of view, it is easy using the exact sequence $0 \rightarrow \mathbb{Z} \rightarrow \mathbb{R} \rightarrow U(1) \rightarrow 0$, from the classifying space point of view it results from the fact that $BU(1)=\mathbb{C}P^\infty$) but the easiest gauge theoretic understanding is that if you have a 2-cycle $C$ in $M$ defining a non-zero element in $H_2(M, \mathbb{Z})$, then you can construct a non-trivial gauge configuration by asking for a non-zero flux $\int_C F$ of the field strength through $C$. By flux quantization, we necessarely have $n= 1/(2 \pi) \int_C F \in \mathbb{Z}$. So if $C_1$, ..., $C_k$ is a basis of $H_2(M, \mathbb{Z})$ (assume for simplicity that there is no torsion) then a $U(1)$ principal bundle is naturally given by a collections of integers $(n_1,..., n_k)$, i.e. by an element in $H^2(M, \mathbb{Z})$ which is called the first Chern class of the $U(1)$ principal bundle.
The description of $U(1)$ principal bundles in terms of $H^2(M, \mathbb{Z})$ is true whatever is the dimension of the spacetime $M$. The point is that $U(1)$ is a very simple group. For examples, its homotopy groups are simple: $\pi_1 (U(1))=\mathbb{Z}$, $\pi_i(U(1))=0$ for all $i \geq 2$. All the fun comes from $\pi_1$, i.e. that it is possible to construct a non-trivial $U(1)$-principal bundle on a sphere $S^2$ by a non-trivial gluing along the equator $S^1$. If you are in dimension 4, there is no $U(1)$-instanton in the sense of configuration localized around a point but if you take as definition of instanton a non-trivial (anti) self-dual solution, then existence of instanton requires a non-trivial $H^2(M, \mathbb{Z})$ and more precisely one has to look at the action of the Hodge star on $H^2(M, \mathbb{Z})$.
For $G$ a non-abelian compact Lie group, things are very different. Let us assume for simplicity that $G$ is connected and simply connected (for example, $G=SU(N)$). Then there is no general explicit description of $H^1(M, C^\infty (M,G)) = [M, BG]$ for arbitrary $M$ because $G$ has non-trivial homotopy groups in arbitrarily high degree. But if we restrict ourself to $M$ of small dimension then it is possible to say something because the small degree homotopy groups of $G$ are not too complicated $\pi_1(G)=0$, $\pi_2(G)=0$, $\pi_3(G)=\mathbb{Z}$. One can show that this implies that every $G$-principal bundle is trivial if $M$ is of dimension less or equal to 3 and that if the dimension is equal to 4, then the $G$-principal bundles on $M$ are classified by an integer $n \in \mathbb{Z}$, called the instanton number (which is the second Chern class if $G=SU(N)$). Here I am assuming $M$ compact. So the result is the same whatever is the topology of $M$. The point is that in this case, all the fun comes from $\pi_3$, i.e. from the possibility to construct a non-trivial $G$-principal bundle on the sphere $S^4$ by non-trivial gluing along the equator $S^3$. This is a local phenomenon which can be inserted around any point of any four manifold. This locality is related to the fact that instanton configurations of the gauge field are in general localized around some finite number of points. The compacity of $M$ is only required to be sure that the inserted local non-triviality is no untwisted by a non-trivial operation at infinity. But from a physical point of view, non-trivial operations at infinity are forbidden anyway: gauge transformations at infinity are not redundancies but global symmetries. So even if there is no non-trivial $G$-principal bundle on flat $\mathbb{R}^4$, the relevant objects to consider are in fact $G$-principal bundles with a trivialization at infinity and so we still have instantons. In conclusion, non-abelian instantons have nothing to do with topology of spacetime, they exist in four dimensional Euclidean flat space, but have everything to do with the topology of the non-abelian gauge $G$ and more precisely with the fact that $\pi_3(G)=\mathbb{Z}$.