Motivation behind the definition of ideal class group 
Let $O$ be a Dedekind domain and $K$ its field of fractions. The set of all fractional ideals of $K$ form a group, the ideal group $J_K$ of K. The fractional principal ideals $(a) = aO, a \in K^*$, form a subgroup of the
  group of ideals $J_K$, which will be denoted $P_K$. The quotient group
  $$CL_K=J_K/P_K$$ is called the ideal class group of $K$.

This definition looks completely arbitary, I fail to see why the class group or the ideal class group is worth studying. So, why is the class group or ideal class group interesting? I've read that ideal class group "measures" how much unique factorization fails in a domain, but I don't understand how it does so.
This from Algebraic Number Theory by Neukirch.

The class group $C_K$ measures the expansion that takes place when we pass from numbers to ideals, whereas the unit group $O^*$ measures the contraction in the same process. This immediately raises the problem of understanding these
  groups $O^*$ and $C_K$ more thoroughly.

I don't understand the above paragraph. What does it mean by  "the expansion that takes place when we pass from numbers to ideals"? And why should we even bother about such "expansion"?
 A: The group $K^*$ is interesting. However, it is hard to study.
The group $J_K$ is relatively easy to study.
There is a homomorphism $f:K^* \to J_K$ connecting the two, that sends a non-zero element of $K$ to the principal fractional ideal it generates.
Therefore, we can learn a lot of information about $K^*$ by studying $J_K$ instead.
The drawback is, of course, that $J_K$ is not actually $K^*$. And sometimes, that difference matters. The difference is quantified by two other groups: $\mathcal{O}^*$ is the kernel of $f$, and $CL_K$ is the cokernel of $f$. If we can obtain knowledge about these two additional groups, that helps us understand the difference between $K^*$ and $J_K$.

That said, sometimes $CL_K$ is interesting in its own right, and sometimes $J_K$ is the group we're actually interested in. We might even be specifically interested in the map $f$! Even in those cases, knowledge of the other groups can still help.
A: Let $K$ be an algebraic number field and $\mathcal{O}_K$ the integral closure of $\Bbb{Z}$ in $K$. Let us recall that a Dedekind domain is a UFD iff it is a PID. Classically, I think that the questions concerning whether or not a certain Dedekind domain was  a UFD were very important, see e.g. this thread here. 
Perhaps from the point of view of algebra asking if something is a PID is easier to approach: we know how to factor ideals in Dedekind domains and thus there should at least be a tool to measure how far does a Dedekind domain differ from being a principal ideal domain. 
Let me now give you an alternative definition of the ideal class group. We will put an equivalence relation on the set of all ideals defined as follows. We say that an ideal $I$ of $\mathcal{O}_K$ is equivalent to $J$ iff there is $\alpha,\beta \in \mathcal{O}_K$ so that 
$$\alpha I = \beta J.$$
One easily checks that this is an equivalence relation. With a little bit more work, one can show that the set of all equivalence classes has a well defined multiplication law and is actually a group. The identity element being the class of all principal ideals. Now for some exercises.


Exercise 1: Check that the "class" of all principal ideals is actually a class. Namely if $I$ is an ideal such that $\alpha I = (\beta)$ then show that $I$ is actually principal. Hint: $\mathcal{O}_K$ is an integral domain.
Exercise 2: Show that this definition of an ideal class group is actually equivalent to the one given in Neukirch. Hint: Use the first isomorphism theorem. 


Now do you see how the definition I have given you of an ideal class group actually measures nicely whether or not $\mathcal{O}_K$ is a PID? We see that $\mathcal{O}_K$ is a PID iff its ideal class group is trivial. 
Now on to more interesting material. In advanced subjects such as Class Field Theory one can construct something know as the Hilbert Class Field of $K$. I don't know all the details of this construction as my algebraic number theory is not so advanced, but in the Hilbert class field every ideal of $\mathcal{O}_K$ becomes principal!! One can now ask the question: can we avoid talking of the Hilbert class field and find such an extension?
The answer is: Of course we can! This is where the ideal class group comes in. Firstly from Minkowski's bound we get that $Cl_K$ is actually a finite group. Using this, here are now two exercises which you can do:


Exercise 3: Let $I$ be an ideal of $\mathcal{O}_K$ show that there is a finite extension $L/K$ so that $I\mathcal{O}_L$ is principal. Hint: By finiteness of the class group there is an $n$ so that $I^n = \alpha$ for some $\alpha \in \mathcal{O}_K$. Now consider $L = K(\sqrt[n]{\alpha})$. 
Exercise 4: Show that there is a finite extension of $L$ in which every ideal of $\mathcal{O}_K$ becomes principal. Wowowowowow!
     Hint: Use exercise 3 and the definition of the ideal class group given in the beginning of my answer, not the one in Neukirch. 


If you are stuck with any of these exercises I can post their solutions for you to view here. 
Solution to Exercise 1 (As requested by user Andrew):
Suppose that $\alpha I = (\beta)$. Then in particular there is $x \in I$ so that $\alpha x = \beta$. We claim that $I = (x)$. Now it is clear that $(x) \subseteq I$. For the reverse inclusion take any $y \in I$. Then $\alpha y = \beta \gamma$ for some $\gamma \in R$. Since $\beta = \alpha x$ we get that
$$\alpha y = \alpha x \gamma.$$
But now $\mathcal{O}_K$ is an integral domain and so $y = x\gamma$, so that $I \subseteq (x)$. Hence $I = (x)$ and so $I$ is principal.
A: At a crucial point in the proof that the equation $y^2=x^3-31$ has no solutions in the integers, one has an equation relating ideals, $$((y+\sqrt{-31})/2)=(2,(3+\sqrt{-31})/2)A^3$$ for a certain ideal $A$ in the ring of integers in ${\bf Q}(\sqrt{-31})$. But it turns out that the class number of that ring of integers is $3$, so $A^3$ is a principal ideal, and you get a contradiction, since you can prove that $(2,(3+\sqrt{-31})/2)$ is not a principal ideal. 
Needless to say, this is just one example among many where class group properties help solve Diophantine equations. 
A: We have the short exact sequence of groups
$$1\to k^\times\to J_k\to \text{cl}(k)\to 1$$
The issue with this is that we have been forced to go to "group land" because we don't want to deal with monoids (e.g. the monoid of $\mathcal{O}_k$ under multiplication). So, let's forget that we have taken monoids at each step and done some kind of fractional process to get groups. What we end up with is
$$1\to \mathcal{O}_k\to \left\{\text{ideals of }\mathcal{O}_k\right\}\to \text{cl}(k)\to 1$$
So, we see that the class group can be roughly though about as the ratio of $\{\text{ideals of }\mathcal{O}_k\}$ to $\mathcal{O}_k$ itself. In other words, in our quest for unique factorization we have passed from elements of $\mathcal{O}_k$ (which may not be a UFD) to ideals of $\mathcal{O}_k$ (which always do have unique factorization). In a perfect world of sunshine and lollipops the ideals of $\mathcal{O}_k$ would be the same as $\mathcal{O}_k$ because $\mathcal{O}_k$ would be a PID. Alas, we live in no such sweet world and in general we are going to have to "add in extra" to get the unique factorization we need. The ratio of $\left\{\text{ideals of }\mathcal{O}_k\right\}$ to $\mathcal{O}_k$ (i.e the class group) measures just how much extra we had to add in. 
A: The concept of ideal entered the world of mathematics somewhere in the 19th century, I believe in the context of the ring of cyclotomic integers $\Bbb Z[\zeta_n]$, where $\zeta_n=e^{2\pi i/n}$, in the attempt to attack Fermat's last theorem.
An ideal is a generalization of a number--or more generally speaking of an element of a ring--because if $x\in{\cal O}_K$ the subset $(x)=x{\cal O}_K$ is indeed an ideal. The concept became soon useful in other contexts, e.g. in geometry where the set of polynomial functions vanishing on an algebraic set in affine space is actually an ideal, and so on.
The motivation behind the introduction of ideals, at least in an arithmetic context, is that a ring of algebraic integers ${\cal O}_K$ is a Unique Factorization Domain (and in particular the notions of irreducible and prime elements coincide) if and only if every ideal $I$ is actually principal, i.e. $I$ is one of those ideal generated by, or "coming from" a single number as before: $I=(x)$.
Actually, a less strict requirement on the ideals of $\Bbb Z[\zeta_p]$ for $p$ prime was devised by Kummer to show the unsolvability, when the requirement is met, of Fermat's equation with exponent $p$.
These are obvious clues that the set of ideals is worth studying.
So, a first natural question is: "How much do we enlarge the set of elements when we generalize from elements to ideals"? Basically, the idea is that the more the set of ideals is relatively bigger, the more "complicated" the arithmetic of ${\cal O}_K$ is. Since both sets are infinite, the answer to this question cannot be too straightforward.
The key observation is that the ordinary multiplication in $K$ induces a multiplication between ideals which in fact "extends" the obvious multiplication $(x)\cdot(y)=(xy)$ between principal ideals. It is remarkable that under this extended multiplication the uniqueness of factorization is recovered: ideals can be uniquely decomposed as  aproduct of prime ideals, and a prime principal ideals are exactly those generated by prime elements. Moreover, if we further generalize our picture introducing fractional ideals, these multiplications actually define a structure of abelian group on the set $J_K$ of fractional ideals for which the subset $P_K$ of principal fractional ideals is a subgroup. 
But now that we have groups there's a very natural way to compare sizes: just take the quotient $C_K=J_K/{\cal O}_K$.
So the fundamental result that $C_K$ is finite means that when we generalize from elements to ideals we do deal with a bigger set, but not that relatively bigger. Or, in other words, that for a generic number field $K$ the arithmetic of ${\cal O}_K$ may be essentially more complicated than that of $\Bbb Z$ but after all not that more complicated (warning: this last interpretation may be read too optimistically and lead to false expectations; :-) )
