Many different parties had many different goals.
Officially, it was in response to the Serbian government refusing part of the Austro-Hungarian (I'm gonna shorten that to AH) ultimatum following the assassination of the heir apparent by a Bosnian conspiracy backed and trained by officials in the Serbian government and military. But that ultimatum was purposefully ludicrous as it was never meant to be accepted.
Some of the demands were that Serbia cease her protest over the AH annexation of Bosnia and Herzegovina in 1908, which had been under their control since the Treaty of Berlin in 1878 while still officially part of the Ottoman Empire until the 1909 amendment to the treaty and that she remove and suppress anti AH propaganda. Which was fine actually, the Serbian government would have agreed to that. But then it was also demanded that Serbia remove and put to trial all administrative and military officials that AH name and allow Austro-Hungarian officials to conduct investigations on Serbian soil, both of which were understandably unacceptable. Full text here.
If we delve into deeper reasons we first find a group of very war-hungry ministers. Chief of General Staff Franz Conrad von Hötzendorf, Minister of War Alexander von Krobatin and Prime Minister of the Austrian half Karl Stürgkh all had urged for war with Serbia for years because they felt that it was the only (or at least best) way to decrease the rising panslavism, the desire of the Slavic subjects to unite under a free country like Serbia. Foreign Minister Leopold Berchtold, Emperor Franz Joseph and his heir Franz Ferdinand on the other hand were all very much opposed to the idea for their own reasons.
Franz Joseph mostly because he was really old and tired of war at that point and preferred to retreat into his own little idyllic fantasy world. Franz Ferdinand was inventor and vocal proponent of extending the double monarchy into a tripple monarchy (trialism) and giving the Slavs the same privileges his uncle gave to the Hungarians in 1867, maybe even further into something like the United States. Berchtold is the most interesting - he wasn't pro-Serbian at all, in fact he was a major factor in the creation of Albania to cut the Serbs off from a coastline and strived to hold down and annex Serbia through diplomatic means but he always opposed the idea of waging a war against them. Then, after the assassination, he suddenly turned around and became a leading figure of the pro-war Kriegspartei (war party), although even then he was the calm one. Von Hötzendorf on the other hand almost immediately started a personal campaign of getting war declared as early as possible, something the Austro-Hungarian army could never have handled.
In fact, most of the ministers eventually succumbed to the idea of war, in the end it was really only the Hungarian Prime Minister István Tisza who stood firmly opposed. Mostly because he didn't want Serbia added to the Empire as that would have meant an even bigger Slavic population in his half and thus further sabotage the Magyar supremacy, but at least it's something...
If we're looking at Germany, there were a lot of worries about the neighbours. France in the West and Russia in the East both were allied and both had long worked towards modernisation and mobilisation of their armies. It was thought that both parties were planning for war with Germany at around 1916 but Chief of German Staff Helmuth Johannes Ludwig von Moltke did not expect Germany to be able to win a war by that point, so there had been talks about pre-emptive strikes for years. Most famously the German Imperial War Council of 1912. The conflict with Serbia was seen as a a very convenient way for Germany to get into a war with Russia, thus Kaiser Wilhelm's blank cheque to support Austria-Hungary in an aggressive war even if it turned into a European conflict. In Austria the same feelings were present, although much less prominent.
Historian Eric A. Leuer also suggests that part of the reason for the war was a hope that a large conflict would unite the splintered nationalities of the crumbling dual monarchy against a common external enemy.
Sources:
Ludwig Bittner, Hans Uebersberger; Österreich-Ungarns Außenpolitik von der bosnischen Krise 1908 bis zum Kriegsausbruch 1914: Diplomatische Aktenstücke des österreichisch-ungarischen Ministeriums des Äußeren
Annika Mombauer; Helmuth Von Moltke and the Origins of the First World War
Lazlo Csorba, John C. Swanson, Peter Hanak, et al.; Österreich-Ungarn, Das Habsburgerreich von 1867 bis 1918
Edit: some spelling and bad grammar I didn't notice yesterday
A very important question- the "blame game" was one of the biggest questions historians pondered over following the Great War. A case can be made for each of the five major players being the most responsible (Germany, AUstro-Hungary, France, Russia, Great Britain), but a lot believe that Austro-Hungary was most responsible.
After Franz Ferdinand was assassinated, the leaders of the Austro-Hungarian empire wanted to put an end to Serbian agitation once and for all. To do this, they needed to be able to go to war, and to assure Germany's support in case Russia entered to protect Serbia. Why did they want to go to war with Serbia so bad? Not because they were genuinely angry for Ferdinands death, but because as a multi-ethnic empire in decline, it wanted to take a firm stance against nationalist challenges. After the retreat of the Ottoman Empire from the Balkans, Austro-Hungary got embroiled in a series of wars, and in 1908, annexed bosnia-herzogavania. This, on top of the increased magayarization, and nationalist movements in Transylvania, Bohemia, Moravia, etc., threatened to pull the empire apart. The nation had serious economic problems on top of the political ones. A strike against Belgrade could be seen as a way to "save face" and show the strength of the declining empire. When Germany assured Austro-Hungary of their support (why they decided to do this is a whole nother question entirely), the leaders of AH gave Belgrade an offer they couldn't accept (pretty much forced Serbia into a position where they went to war) and didn't consider the Russian reaction all too much. They hoped it would be a short war that proved the Empire still could be aggressive and would take a strong stance against nationalist challenges.
A good book, of which there are many, is The Origins of WWI- Hamilton and Herwig. It devotes a chapter to the considerations of every combatant leading up to their entrance in the war.