A loop on the circle is determined by one integer, the number of times it winds around. On the torus it takes two integers, and on the figure eight a word written with two letters. The group formed by those integers or words is the fundamental group of the space [3].
For a space X with base point x_0, the fundamental group \pi_1(X, x_0) is the set of homotopy classes of loops that start and end at x_0, with the product given by running one loop after the other [3]. When X is path-connected the group does not depend on the base point, and we simply write \pi_1(X).
Three tools do the computations below: lifting paths to a covering space, splitting a product into its factors, and the van Kampen theorem for spaces glued together from pieces.
Let p : \tilde{X} \to X be a covering: every point of X has a neighbourhood whose preimage is a disjoint union of open sets, each carried homeomorphically onto it by p [1].
Two properties are needed [1,2]. A path f starting at x_0 has exactly one lift \tilde{f} starting at a chosen point \tilde{x}_0 \in p^{-1}(x_0), and a homotopy of paths f_t starting at x_0 has exactly one lift \tilde{f}_t starting at \tilde{x}_0.
Both are proved the same way. Cover X by evenly covered open sets, cut the interval into small pieces and lift one piece at a time. The interval is compact, so finitely many pieces are enough [1].
Take p : \mathbb{R} \to S^1, p(s) = (\cos 2\pi s, \sin 2\pi s), a covering with p^{-1}(1, 0) = \mathbb{Z} [1]. Lift a loop f at (1, 0) to the path \tilde{f} that starts at 0. Its end lies over (1, 0), so it ends at an integer n.
The loop \omega_n(s) = (\cos 2\pi n s, \sin 2\pi n s) lifts to \tilde{\omega}_n(s) = ns. Both lifts run from 0 to n in \mathbb{R}, so (1 - t)\, \tilde{f} + t\, \tilde{\omega}_n joins them, and composing with p gives f \simeq \omega_n. Every class is some [\omega_n].
Conversely, if \omega_m \simeq \omega_n, lift the homotopy starting at 0. By uniqueness it runs from \tilde{\omega}_m to \tilde{\omega}_n, and its endpoint cannot move, so m = n [1]. Hence \pi_1(S^1) \cong \mathbb{Z}: the winding number is the only homotopy invariant of a loop.
A map Z \to X \times Y is continuous exactly when both of its components are, so a loop in X \times Y is a pair of loops and a homotopy is a pair of homotopies. This gives \pi_1(X \times Y) \cong \pi_1(X) \times \pi_1(Y), a homomorphism because loops are joined componentwise [1].
For the torus T^2 = S^1 \times S^1 this yields \pi_1(T^2) \cong \mathbb{Z}^2 [2]. The generators are the loop \alpha, which goes once around the first factor, and the loop \beta, which goes once around the second.
The group is commutative, so \alpha\beta = \beta\alpha and every loop can be written as \alpha^m \beta^n. Lifted to the plane \mathbb{R}^2, which covers the torus, such a loop becomes the segment from (0, 0) to the lattice point (m, n). Repeating the argument gives \pi_1(T^n) \cong \mathbb{Z}^n.
The figure eight S^1 \vee S^1 is not a product, so another tool is needed: the van Kampen theorem. Thicken each circle slightly to open sets A_1 and A_2. Each deformation retracts onto its circle, and A_1 \cap A_2 shrinks to a point [1].
Since the intersection is simply connected, the theorem gives a free product, \pi_1(S^1 \vee S^1) \cong \mathbb{Z} * \mathbb{Z} \cong F_2, the free group \langle a, b \rangle on two letters [1]. No relation is imposed, because the gluing takes place over a set with trivial fundamental group.
So ab \neq ba: running the two loops in different orders gives different homotopy classes. A wedge of k circles has the free group F_k on k letters as its fundamental group.
Write \mathbb{RP}^n = S^n / (x \sim -x) and let p : S^n \to \mathbb{RP}^n be the quotient map, a two-sheeted covering [2]. For n \geq 2 the sphere is simply connected, so p is the universal covering, and the fundamental group is then isomorphic to the group of deck transformations [2].
The deck transformations are the identity and x \mapsto -x, so \pi_1(\mathbb{RP}^n) \cong \mathbb{Z}/2\mathbb{Z} for n \geq 2. The case n = 1 is different: \mathbb{RP}^1 is a circle, with group \mathbb{Z}.
A nontrivial loop is easy to draw. Take half a great circle from the north pole to the south pole. Its ends are identified, so it closes up in \mathbb{RP}^n. Its lift does not close, so it is not trivial, but traversed twice it lifts to a closed loop on the sphere, which contracts.
For n \geq 2 we have \pi_1(S^n) = 0 [1]. Rather than shrinking loops directly, it is quicker to split the sphere. Removing the north pole gives A_1 and removing the south pole gives A_2. Each is homeomorphic to \mathbb{R}^n, and A_1 \cap A_2 is homeomorphic to S^{n-1} \times \mathbb{R}, which is path-connected when n \geq 2.
A lemma does the rest. If X is a union of path-connected open sets that contain the base point and have path-connected intersections, then every loop at the base point is homotopic to a product of loops, each staying inside one of the sets [1]. The proof cuts the interval finely and uses compactness.
Here every loop inside A_1 or A_2 lives in a copy of \mathbb{R}^n and contracts, so every loop on S^n contracts. For n = 1 the argument fails, because A_1 \cap A_2 falls into two pieces.
The space \mathbb{CP}^n is built by attaching a cell of dimension 2n to \mathbb{CP}^{n-1}, over and over, so \mathbb{CP}^n = e^0 \cup e^2 \cup \cdots \cup e^{2n} [1].
Every cell has even dimension, so the 1-skeleton is the single 0-cell, a point. For a path-connected CW complex the inclusion of the 1-skeleton induces a surjection on \pi_1 [1], and the fundamental group of a point is trivial. Hence \pi_1(\mathbb{CP}^n) = 0.
Real projective space is the opposite case. It has one cell in every dimension, so its 1-skeleton is a circle, and that is where \mathbb{Z}/2\mathbb{Z} survives.
The closed orientable surface M_g of genus g is made by gluing the sides of a 4g-gon in pairs. It has one 0-cell, 2g 1-cells and one 2-cell [1]. The 1-skeleton is a wedge of 2g circles, with a free group on 2g letters, and the word along which the 2-cell is attached becomes the single relation.
So \pi_1(M_g) \cong \langle a_1, b_1, \ldots, a_g, b_g \mid [a_1, b_1] \cdots [a_g, b_g] \rangle [1]. For g = 1 the relation [a_1, b_1] says a_1 b_1 = b_1 a_1, which is the \mathbb{Z}^2 of the torus again.
For g \geq 2 the group is not commutative. Making it commutative gives \mathbb{Z}^{2g}, so different genera give different groups, and surfaces of different genus are not even homotopy equivalent. The Klein bottle, glued by the word a b a^{-1} b, has \pi_1(K) \cong \langle a, b \mid a b a^{-1} b \rangle.
Having seen these examples, one may ask which groups occur. The answer is all of them [1]. Take a presentation G = \langle g_\alpha \mid r_\beta \rangle. Every group has one, since every group is a quotient of a free group.
Take one circle for each generator and form their wedge, then attach a 2-cell along the word of each relation r_\beta. The resulting two-dimensional CW complex X_G has fundamental group G [1]. The wedge supplies the generators and the cells supply the relations, exactly as for the figure eight and the closed surfaces.
So the fundamental group alone cannot pin down the kind of space. In the other direction, any question about a group can be turned into a question about a space.
The circle gives \mathbb{Z}, the sphere S^n with n \geq 2 gives the trivial group, the torus T^n gives \mathbb{Z}^n, the figure eight gives F_2, the space \mathbb{RP}^n with n \geq 2 gives \mathbb{Z}/2\mathbb{Z}, and \mathbb{CP}^n is simply connected.
The commutative answers come from products and from surfaces glued with commutative relations. The non-commutative ones come from free products and from the relations of closed surfaces of genus g \geq 2.
Only three tools were used: lifting, splitting into factors and gluing. Once the way a space is built is known, its fundamental group follows the same construction.