Why was Sheaf cohomology invented? Sheaf cohomology was first introduced into algebraic geometry by Serre. He used Čech cohomology to define sheaf cohomology. Grothendieck then later gave a more abstract definition of the right derived functor of the global section functor.
What I still don't understand what was the actual motivation for defining sheaf cohomology. What was the actual problem they were trying to solve?
 A: Sheaves and sheaf cohomology were invented not by Serre, but by Jean Leray while he was a World War II  prisoner in Oflag XVII (Offizierlager=Officer Camp) in Austria.
After the war he published his results in 1945 in the Journal de Liouville.
His remarkable but rather obscure results were clarified by Borel, Henri Cartan, Koszul, Serre and Weil in the late 1940's and early 1950's.    
The first spectacular application of Leray's new ideas was Weil's proof of De Rham's theorem: he computed the  cohomology of the constant sheaf $\underline {\mathbb R}$ on a manifold $M$ through its resolution by the acyclic complex of differential forms $\Omega_M^*$ on $M$.     
The next success story for sheaves and their cohomology was the proof by Cartan and Serre of theorems $A$ and $B$ for Stein manifolds, which solved a whole series of difficult problems (like Cousin I and Cousin II) with the help of techniques and theorems of Oka, who can be said a posteriori to have implicitly introduced sheaves in complex analysis.
The German complex analysts (Behnke, Stein, Thullen,...) who had up to then be the masters of the field  were so impressed by the new cohomology techniques that they are reported to have exclaimed: "the French have tanks and we have bows and arrows!"     
Armed with his deep knowledge of these weapons of complex analysis Serre took the incredibly bold step of introducing  sheaves and their cohomology on algebraic varieties endowed with their Zariski topology.
This was of remarkable audacity because of the coarseness of Zariski topology, which had led specialists to believe that it was  just some rather unimpressive tool allowing one for example to talk rigorously of generic properties.
As all algebraic geometers now know, Serre stunned his colleagues  by showing in FAC how cohomological methods yielded deep results, at the centre of which are theorems $A$ and $B$ for coherent sheaves on affine varieties.
Other fundamental novelties obtained by Serre in FAC are his twisting sheaves $\mathcal O(n)$, the computation of the cohomology of coherent sheaves on projective space, the vanishing of the cohomology groups $H^q(V,\mathcal F(n))$ on a projective variety $V$ for $q\gt0, n\gt\gt 0$,...     
Last not least: the introduction of sheaves and their cohomology in FAC paved the way for Grothendieck's revolutionary introduction of schemes in algebraic geometry, as acknowledged in the preface of EGA.
Actually reading FAC was the secondary thesis of Grothendieck, accompanying his PhD on nuclear spaces in functional analysis.
Afer his PhD defence, someone (Cartan if I remember the anecdote correctly) told him good-humouredly that he seemed not to have understood much in FAC.
The story goes that Grothendieck was piqued, invested much energy in understanding Serre, and the rest is history. Se non è vero, è ben trovato...
A: Sheaf cohomology is just the elaboration of the following problem:

you have a space and a covering, and you can do something you want on each set of the covering: can you do it on the whole space?

This occurs in many places, from the Cousin problem in complex analysis to the construction of sections of fiber bundles to the pasting together of solutions of partial differential equations which you can solve locally to many, many other contexts.
In a way, once you are caught in a situation where you want to piece local information to obtain global information, you are more or less doomed to develop sheaf cohomology.
A: Hartshorne says that cohomology was first introduced to abstract algebraic geometry by Serre in his Faisceaux Algebriques Coherents paper (translated to English by a friend of mine). The FAC says

We know that the cohomological methods, in particular sheaf theory, play an increasing
role not only in the theory of several complex variables ([5]), but also in
classical algebraic geometry (let me recall the recent works of Kodaira-Spencer
on the Riemann-Roch theorem). The algebraic character of these methods suggested
that it is possible to apply them also to abstract algebraic geometry; the
aim of this paper is to demonstrate that this is indeed the case.

So, it seems that the motivating reason was the general success of homological methods in various branches of algebra, topology and complex geometry.
For a specific problem that asked for proper cohomology theory, read a discussion on Weil conjectures in Hartshorne's "Algebraic Geometry".
