A CW complex is a topological space built inductively by attaching cells (disks) of increasing dimension via continuous maps on their boundaries. Starting from a discrete set of points (0-skeleton), we attach 1-cells (intervals) along their endpoints, then 2-cells (disks) along their boundary circles, and so on. CW complexes are the natural habitat of algebraic topology: they are general enough to model all reasonable topological spaces (every manifold, every polyhedron, every simplicial complex), yet structured enough that homology, cohomology, and homotopy groups can be computed efficiently via cellular methods.
A CW complex is built by an inductive process of cell attachment. Start with a discrete set X^0 of points (the 0-skeleton). The n-skeleton X^n is obtained from X^{n-1} by attaching n-cells: for each n-cell e_alpha^n, take a copy of the closed n-disk D^n and glue it to X^{n-1} along a continuous attaching map phi_alpha : S^{n-1} = boundary(D^n) -> X^{n-1}. The resulting space is X^n = X^{n-1} union_{phi_alpha} D^n (a quotient of the disjoint union that identifies each point of S^{n-1} with its image under phi_alpha). The CW complex X is the union X = union X^n with the weak topology: a set is open in X if and only if its intersection with every X^n is open.
The key feature of CW complexes is the flexibility of the attaching maps. In a simplicial complex, simplices must be glued face-to-face with all vertices distinct. In a CW complex, the attaching map phi : S^{n-1} -> X^{n-1} can be any continuous map — it can collapse part of the boundary, wrap it around multiple times, or map it to a lower-dimensional skeleton. This flexibility allows extremely economical cell structures. The sphere S^n needs only two cells (one 0-cell and one n-cell), the torus needs one 0-cell, two 1-cells, and one 2-cell, and complex projective space CP^n needs just one cell in each even dimension up to 2n.
The cellular chain complex of a CW complex gives the most efficient route to computing homology. The n-th cellular chain group is the free abelian group on the n-cells: C_n^{CW}(X) = Z^{number of n-cells}. The cellular boundary operator d_n : C_n^{CW} -> C_{n-1}^{CW} is determined by the degrees of the attaching maps: the coefficient of e^{n-1}_beta in d_n(e^n_alpha) is the degree of the composite map S^{n-1} -> X^{n-1} -> X^{n-1}/X^{n-2} = wedge of (n-1)-spheres -> S^{n-1}_beta. The resulting homology H_n^{CW}(X) is isomorphic to the singular homology H_n(X). For CP^n, the cellular chain complex is 0 -> Z -> 0 -> Z -> 0 -> ... -> Z -> 0, and all boundary maps are zero (since there are no adjacent-dimension cells), immediately giving H_{2k}(CP^n) = Z.
Two fundamental theorems in homotopy theory demonstrate the centrality of CW complexes. Whitehead's theorem states that a continuous map between CW complexes that induces isomorphisms on all homotopy groups is a homotopy equivalence. This fails for general spaces but holds for CW complexes because of their inductive cell structure. The CW approximation theorem states that every topological space is weakly homotopy equivalent to a CW complex — meaning there exists a CW complex with the same homotopy groups, homology groups, and cohomology groups. Together, these theorems mean that for the purposes of algebraic topology, CW complexes are the canonical class of spaces: every "algebraic topology question" can be answered within the world of CW complexes.