How is the codomain for a function defined? Or, in other words, why aren't all functions surjective? Isn't any subset of the codomain which isn't part of the image rather arbitrary?
 A: Here is an answer which directly addresses the question in the title:  the codomain has to be given as part of the information telling you what the functions is; it can't be deduced (or in the language of question, "defined") if all you know is the domain and values of
the function.  It is an extra piece of data.  (This is why it seems arbitrary to you; you are thinking about how to determine the codomain from the other data, which can't be done!
You have to be told what it is as part of the initial description of the function.)
First note that this is incompatible with one traditional definition of a function as being a set of ordered pairs.  The set of ordered pairs definition determines the domain and values of the function, but not the codomain.   (I guess that some people do use this definition of function; for them, a function doesn't have a codomain separate from its image.)
To define a function which has a domain and a codomain, one should instead use the scheme
described by Ryan: a function is a triple (domain $A$, codomain $B$, set of elements of
$A\times B$ determining its values).
As to why we introduce the concept of codomain, Qiaochu's answer describes this.
A: Yes it is arbitrary, and in a sense this is exactly the problem. When we have a function we would like to be able to talk about the inverse of the function (assuming it is 1-1.) 
Consider the following: I have some function $f$ that maps $\mathbb{N} \rightarrow \mathbb{R}$ such that $$f(x) = (e^\sqrt{3.7x}+137.2)^3$$ The point here is that it is much easier to say that my codomain is $\mathbb{R}$ and my function is not surjective than to explicitly list the image of my function and say that $f$ is surjective on this subset of $\mathbb{R}$.
I know that every output will be a real number, but it is hard to say exactly which numbers I will get out of $f$ and which real numbers I can never get. To get to the point, if my function was surjective I know that I could plug any real number into $f^{-1}$. But I know it's not and I therefore have to be very careful with my inputs to $f^{-1}$, restricting my attention only to $f$'s image (otherwise the inverse is undefined.) In this case, we say that $f$ does not have the 'nice' property of surjectivity.
A: Often what is important is not functions but collections of functions, especially subsets of the set of functions between two sets $X$ and $Y$, some of which will be surjective and some of which won't.  What also doesn't get emphasized a lot at the level of elementary set theory is the compositional structure of functions, e.g. a function $f : X \to Y$ can be composed with a function $g : Y \to Z$ to give a function $fg : X \to Z$.  For the purposes of studying this compositional structure (for example if $X = Y = Z$) it is generally important not to require that $f$ and $g$ be surjective or you will miss out on structure.  
For example, a simple way to define a dynamical system is just as a function $f : X \to X$.  Whether $f$ is surjective or not is an important aspect of classifying the dynamics of $f$, or in other words of classifying the behavior of the sequences $\{ x, f(x), f^2(x), f^3(x), ... \}$ for various $x$.  This sequence is not well-defined if you pretend that the domain and codomain of $f$ are different just because the range and the codomain aren't equal.
This is another way of saying that a function really consists of three pieces of data (a domain, a codomain, and the mapping from one to the other), but the reason these three pieces of data are all important is really the compositional structure.  
A: This is something that trips people up the more they dwell on it.  And depending on how you like to think about mathematical objects, you have different preferred answers.  
More often than not, I tend to think of a function as consisting of three things: a domain $A$, a range $B$ and a subset $S$ of $A \times B$ which satisfies this condition, that for any $a \in A$ there is a unique $b \in B$ such that $(a,b) \in S$.  To be precise, a "function" $f$ is a triple $(A,B,S)$ where $S \subset A\times B$ satisfies the above criterion. We say $f : A \to B$ and $f(a) = b$ provided $(a,b) \in S$. 
In the above formalism, you can't describe a function without specifying the domain and range before hand. To be specific, here are two different ways of specifying the function that in a calculus class would just be called $x^2$:  $f=(\mathbb R, \mathbb R, \{(x,x^2) : x \in \mathbb R\})$.   $g=(\mathbb R, [0,\infty), \{(x,x^2) : x \in \mathbb R\})$.  So in this formalism, $f$ is not an onto function, while $g$ is an onto function.  Although $f(x)=g(x)$ for all $x$ in the domain of $f$ and $g$, $f \neq g$ since they have different ranges. 
If you don't use a formalism like the above, yes, the range (or co-domain as you say) is just any arbitrary set containing the image.  Frequently this is taken care of by considering the range to be a typical "universal" set. In a standard calculus course what happens is people never really mention the range, they only talk about the image. 
A: In the same sense you could ask why all functions aren't total functions. The real question is perhaps "what is the use of writing $f:\ S\rightarrow T$" or even more general "what is the use of writing "$v \in S$"?
I'd expect that in most cases where it is mentioned explicitly it's a way to say "this object belongs to this class of objects" and is done because it belonging to that class is somehow considered relevant or useful at that point. This might sound a bit obvious, but I don't think there is much more to it.
A: As far as I know, relations can be defined in two ways:


*

*Given two sets $A$ and $B$, any subset $R$ of $A\times B$ is a relation. The domain is $\{x\in A:\exists y(y\in B,(x,y)\in R)\}$. The codomain is $\{y\in B:\exists x(x\in A,(x,y)\in R)\}$. Obviously the domain is a subset of $A$ and the codomain is a subset of $B$. Not every relation is surjective.

*Every set that only contains ordered pairs is a relation. The domain is $\{x\in\bigcup\bigcup R:\exists y((x,y)\in R)\}$. The codomain is $\{y\in\bigcup\bigcup R:\exists x((x,y)\in R)\}$. Every relation is surjective.


My preferred definition is the first one, but you may like the second one, instead.
