I am giving you an answer from the path integral quantization point of view.
When we quantize a gauge theory, we need to sum over all the configurations of connections of a gauge group on some manifold modulo gauge transformation.
In contrast to the affine space of all connections (without dividing by gauge transformation), the latter space (of connections modulo gauge transformation) can disconnected corresponding to different sectors of bundles which cannot be deformed into each other by any combination of diffeomorphism of the base manifold or continuous deformations of the transition functions of the fiber.
In quantum theory, we absolutely need to sum over all topological sectors in the path integral. For example if we do not do that in the problem of a particle moving on a circle, we do not get the correct answer given by Schrödinger's equation.
Chern and Weil (please see the following expositionby Fecko) discovered a profound theorem that there exist topological invariants differentiating between the principal or vector bundles, with the same structure group, which can be expressed by means of certain polynomials of the curvature of the connection. These topological invariants do not depend on the connection (they are gauge invariants) nor on the field strengths but only on the bundle. Moreover, these topological invariants -named characteristic classes- can be expressed by means of cohomology classes of the base manifold (this is why they are closed).
Thus, we can use these invariants to weigh different bundles in the path integral because they do not depend on the connections nor the fields but only on the bundles.
There are other topological invariants which cannot be written in terms of differential forms, such as the Stiefel–Whitney classes, on which the existence of fermions on the manifold depends. These invariants also affect the path integral, however, more advanced techniques are needed to take them into account.
It is worthwhile to mention that not every closed form on the base manifold is an image of a characteristic class (or can be written as a combination of characteristic class). Thus, as topological terms, we can consider only special types of closed forms.