Both answers are wonderful, but I have to admit that there's still some gap between my understanding and why modern topologists think of $BG$ in that way.
That being said, you can see why group cocycles/coboudaries are defined in that way by looking at a classical problem, at least for small $n$.
I will lay out a classical problem, and explain how the cocycle/coboundary conditions show up naturally. For my own good, I hope someone will explain how topologists went from this classical problem to the construction of $BG$ (or bar constructions in general).
Anyway, here we start.
Let $K$ and $G$ be two finite groups. You can easily form their trivial product $K\times G$. This is another group, and it helps us to understand group theory a bit more because now we can investigate big groups by breaking them down to smaller ones.
However, not every big group is a trivial product of smaller groups, so following the same thought we want to find more creative products of $K$ and $G$. One easy example all freshmen know is the semi-direct product, provided that $G$ acts on $K$.
It has been successful, so people wondered all possible nontrivial products we can build from $K$ and $G$. This is the so-called group extension problem. More precisely, given two finite groups $K$ and $G$, we want to find all groups $E$, so that $E/K$ is isomorphic to $G$.
It is hard to construct $E$ directly, so let's think in an opposite way. We imagine that we have found such extension $E$. Its group structure is still unknown, but as a set it must be $K\times G$. We will try to describe the group structure of $E$ by looking at the $K\times G$ side. This way has its pros and cons. Pros: it's easier to write things down concretely. Cons: the structure of $E$ might be highly twisted, so writing everything down in terms of $K\times G$ might make things messy.
(Aside: if you have some experience with differential geometry, the way I'm using now is like drawing a map for $E$. $K\times G$, as a map, gives you a convenient coordinate system for you to write things down, but since $E$ is "curved", we need to develop a corresponding calculus on the map side.)
To go between both side, we want to find a good map that sends $K\times G$ to $E$. From $K$ to $E$ is simple, since by assumption (the "imagination" given above) $K$ already sits in $E$. Unfortunately, for the other side, there's no good map from $G$ to $E$ yet. So lets just pick an arbitrary one $s: G\to E$, so that $s$ serves as a section.
(Aside: I would appreciate if someone can draw a short exact sequence that will make things clearer.. I'm terrible at drawing with latex.)
Note that we cannot expect $s$ to be a group homomorphism! In fact, if that's the case we are back to the semi-direct product, and vice versa (exercise). We want to create more creative products, so we really want some section $s$ that is highly distorted (from being a group homomorphism)!
To measure how distorted it is, we introduce a set-theoretic function $f(g_1,g_2)=s(g_1)s(g_2)s(g_1g_2)^{-1}$. Note that $s$ is not distorted if and only if $f$ is trivial. We want $f$ to be as nontrivial as possible.
Now, after the set-theoretic section $s$ has been chosen, we have a set-theoretic bijection (exercise) between our "space" $E$ and out "map" $K\times G$:
$$ K\times G \to E: (k,g)\mapsto k\,s(g). $$
A fun exercise is to write down how this maps tells us the group structure of $E$. The result is:
$$ (k_1,g_1)(k_2,g_2) = (k_1\, ^{s(g_1)}k_2\, f(g_1,g_2), g_1g_2),$$
where $^xk$ means $xkx^{-1}$, which lies in $E$ because $K$ is a normal subgroup of $E$ by assumption (the imagination made above). It should not be surprising that the distortion measure $f$ shows up in our map. Convince yourself!
Another fun exercise is to translate the associativity on the "space" side to the "map" side: this simply gives you
$$^{s(g_1)}f(g_2,g_3)\,f(g_1,g_2g_3)f(g_1g_2,g_3)^{-1}f(g_1,g_2)^{-1} = 1 \in K.$$
Cocycles
If $K$ is abelian, then $f$ is a $2$-cocycle with the action of $G$ on $K$ given by conjugation. This gives a motivation of why cocycles are defined that way.
(Aside: It should not be surprising why such horrible formula show up, since $s$ is chosen arbitrarily as a set-theoretic map!)
(Aside: Exercise -- what does the other group structures of $E$ translate to? I don't know.. )
Conversely, given a left group action $G$ on $K$ and a $2$-cocycle $f\in Z^2(G,K)$, we can construct a space $E$ simply by reversing the process above. Note that in the space $Z^2(G,K)$ includes the action implicitly.
Here is an explicit construction: set $E$ to be $K\times G$ as a set, and define the group operation by
$$(k_1,g_1)(k_2,g_2) = (k_1\,^gk_2\,f(g_1,g_2),g_1g_2).$$
It is an easy exercise to write down the inverses and the identity element in this set.
Conclusion of the first part: Fix a left group action $G$ on $K$. Any $2$-cocycle $f \in Z^2(G,K)$ gives an interesting product of $K$ and $G$! If $f$ is trivial, then the interesting product is the semi-direct product, which is less interesting.
(Remark: to check that $K$ is central.)
Coboundaries
The next question is of course to find redundancy: do any two given $2$-cocycle $f$ and $h$ give "different" products? If not, how do we identify which two give the same product?
Here, by different I mean that they are not the same. Of course they aren't the same set-theoretically, so I should be clearer: two products $E_f=(K\times G)_f$ and $E_h=(K\times G)_h$ are the same if there is a group isomorphism from one to another that respect all given structures.
So let's suppose $E_f$ and $E_g$ are the same, i.e. there is a good isomorphism between them. The image of $(e,g)_f \in E_f$ in $E_h$ under this isomorphism must be in the form $(k_g,g)_h \in E_h$, for some $k_g \in K$ (exercise). Lets denote $k_g$ by $\phi(g)$ -- it is a function in $g$ -- it is a function $\phi:G\to K$!
The last fun exercise is to show that $f/h$ is the coboundary of $phi$ provided that $K$ is abelian, and vice versa! This exercise is even more fun because you have to find a way to write down the image of $(k,g)_f$ in $E_h$. This gives a motivation of why coboudaries are defined that way.
My questions
How did modern topologist come up with the idea of bar construction from this kind of classical problems? It's kinda sad that nowadays it's so hard to bridge the classical themes to modern considerations.. at least for me!
If we did not assume $K$ to be abelian, we still get something that could be call the nonabelian cocycles/coboudaries, but it is much harder to deal with. How are this done in modern language? And again can you connect the modern treatment of nonabelian cohomology with this classical picture?