When functions commute under composition
Solution 1:
A classic result of Ritt shows that polynomials that commute under composition must be, up to a linear homeomorphism, either both powers of $x$, both iterates of the same polynomial, or both Chebyshev polynomials. Actually Ritt proved a more general rational function case - follow the link. His work was motivated by work of Julia and Fatou's work on Julia sets of rational functions, e.g. see here for a modern presentation.
Solution 2:
According to Wikipedia, a set of diagonalizable matrices commute if and only if they are simultaneously diagonalizable. There is a far-reaching generalization, namely the Gelfand representation theorem.
The Gelfand representation theorem for commutative $C^*$ algebras represents every commutative $C^*$ algebra as an algebra of functions with pointwise multiplication; the domain of the latter algebra is the spectrum of the former algebra.