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 H1(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 H1(M,G) but H1(M,C∞(M,G)) where C∞(M,G) is the sheaf of smooth functions on M with values in G. As mentionned in the comments to the question, we have H1(M,C∞(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 πi(BG)=π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 H1(M,C∞(M,G))=[M,BG]=H2(M,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→Z→R→U(1)→0, from the classifying space point of view it results from the fact that BU(1)=CP∞) but the easiest gauge theoretic understanding is that if you have a 2-cycle C in M defining a non-zero element in H2(M,Z), then you can construct a non-trivial gauge configuration by asking for a non-zero flux ∫CF of the field strength through C. By flux quantization, we necessarely have n=1/(2π)∫CF∈Z. So if C1, ..., Ck is a basis of H2(M,Z) (assume for simplicity that there is no torsion) then a U(1) principal bundle is naturally given by a collections of integers (n1,...,nk), i.e. by an element in H2(M,Z) which is called the first Chern class of the U(1) principal bundle.
The description of U(1) principal bundles in terms of H2(M,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: π1(U(1))=Z, πi(U(1))=0 for all i≥2. All the fun comes from π1, i.e. that it is possible to construct a non-trivial U(1)-principal bundle on a sphere S2 by a non-trivial gluing along the equator S1. 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 H2(M,Z) and more precisely one has to look at the action of the Hodge star on H2(M,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 H1(M,C∞(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 π1(G)=0, π2(G)=0, π3(G)=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∈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 π3, i.e. from the possibility to construct a non-trivial G-principal bundle on the sphere S4 by non-trivial gluing along the equator S3. 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 R4, 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 π3(G)=Z.