When does the set of isometries form a group? Motivation
Its a classic set up. Take a metric space $M$, with distance function $d:M\times M\to \mathbb{R}$. The set of isometries of $M$ is the set of functions $f: M \to M$ which preserve distance. This set has much of the structure of a group without additional assumptions; the composition of two isometries gives an isometry, the identity function takes the place of the identity, etc.
Although every isometry must be injective, however, it is not necessarily a bijection, and so might not have an inverse. For example any injective function from a metric space with the discrete metric ($d:M\times M\to \mathbb{R}, d(a,b) = 1$ if $a \neq b$ and $0$ if $a=b$) to itself is an isometry.
As the group of isometries is quite a useful gadget we can get round this. For example in MathWorld an isometry is assumed to be bijective: http://mathworld.wolfram.com/Isometry.html
Yet in Euclidean space, we do not need any additional assumption:
Lemma
Every isometry of $\mathbb{E}^2$, the Euclidean plane, is a surjection.
Proof
On the plane, for example, assume that a point $a$ does not lie in the image of an isometry $T$. Take three distinct points $T(b_1), T(b_2)$ and $T(b_3)$ that do lie in the image (as $T$ is injective they have a unique preimage). Let $d_i = d(b_i,a)$ be the distance between $d$ and $b_i$.
The three circles radius $d_i$ around $T(b_i)$, intersect together only at $a$,  as the distances between the $T(b_i)$ and the $B_i$ are the same, the circles radius $d_i$ around $b_i$ will also intersect at a unique point $a'$. The point $T(a')$ must be $a$ as that is the only point that satisfies all the point to point distances, so $a$ does lie in the image of $T$.
$\square$
Question
We can extend this argument to higher dimensional Euclidean spaces, yet it uses non-trivial properties, in particular how circles intersect. Is there a simple propetry of a metric space that ensures that the set of isometries forms a group?
Edit To rather strengthen the question, are there simple properties that are necessary and sufficient.
 A: Every isometry between two complete connected Riemannian manifolds of the same dimension is a bijection.
Sketchy proof: 
Let $f:M\to N$ be an isometry between manifolds as above.
I want to show that $f$ is surjective. Given $z\in N$, I want to show that $z\in im(f)$.
Pick a point $x\in M$, and let $y:=f(x)\in N$. Pick a curve $\gamma$ connecting $y$ to $z$.
Since $df$ is everywhere invertible, the map $f$ is a local homeomorphism, and so there is at most one lift of the curve $\gamma\subset N$ to a curve $\tilde \gamma\subset M$. Since the metric in $M$ is complete, the curve $\tilde \gamma$ does exist. Its end point is the desired preimage of $z$.  $\square$
Analysis of what's used in the above proof:
    • Every isometry $M\to N$ is a local homeomorphism.
    • $N$ is path connected.
     • The metric on $M$ is complete.
...and let's not forget:
    • $M$ is non-empty.
Another example where you get your desired result: regular trees.
A: Essentially all you need is surjectivity, which is usually part of the definition of being an isometry. However, if you prefer to keep the definition as you stated above, there are classes of metric spaces for which every isometry is surjective: 
Your example above extends to all Euclidean spaces: in this case, you can classify the group of isometries; every isometry is the composition of an orthogonal transformation and a translation.
If $M$ is a Riemannian manifold, then the Riemannian metric induces a metric $d$ on $M$, by defining $d(p,q)$ as the infimum of the length of all piecewise smooth curves joining $p$ and $q$. In this case, the group of isometries of $(M,d)$ is the same as the group of isometries of $M$ as a Riemannian manifold, and is a Lie group by a theorem of Myers and Steenrod.
Counterexamples to the general case abound: take $M$ to be a Hilbert space, say $l^2$ and 
take the unilateral shift:
$$ \phi(x_1,x_2, \dots)= (0,x_1,x_2, \dots ) $$
which is an isometry, but not surjective.
A: 
You are asking for conditions on a metric space $X$ for which any distance preserving map $X\to X$ is bijective. (Usually isometry is defined as bijective distance preserving map).

Well, there are arbitrarily bad spaces $X$ such that the only distance preserving map $X\to X$ is identity.
In this case distance preserving maps (well, the only one) form the trivial group.
So, I do not see a language which could be used to formulate a necessary and sufficient condition.
For sufficient conditions: 


*

*Compactness;

*Proper + cocompact isometric group action. (proper=bounded closed sets are compact)

*Any complete connected space for which the domain invariance theorem holds; in particular complete connected Riemannian manifolds without boundary. 

