Simultaneously diagonalisable iff A,B commute

Let $\lambda_1,\dots,\lambda_r$ the eigenvalues of $A$ and $E_{\lambda_i}$ the corresponding eigenspace. For any eigenvector $u\in E$, we have $$A(B(u))=B(A(u))=B(\lambda u)=\lambda B(u).$$ This proves the $E_{\lambda_i}$s are stable by $B$.

Now, since $A$ is diagonalisable, the vector space $V$ decomposes as $$V=\bigoplus_{i=1}^rE_{\lambda_i}$$ In a basis of eigenvectors for $A$ the matrix A becomes a diagonal matrix, and the matrix $B$ is a block-diagonal matrix $$\begin{pmatrix} B_1\!\\ &\!\ddots\!\\ &&\!B_r \end{pmatrix}$$ Thus it is enough to observe the restriction of (the endomorphism associated with) $B$ is diagonalisable. Take in each $E_{\lambda_i}$ a basis of eigenvectors of $B_i=B\Bigl\lvert_{E_{\lambda_i}}$. The matrix of the restriction of $A$ to this eigenspace remains diagonal, since it is $\lambda_i I_{E_{\lambda_i}}$. Finally, choose as a basis for $V$ the union of the bases of the $E_{\lambda_i}$. You obtain a basis which diagonalises simultaneously $A$ and $B$.