The rise of Cayley's invariant theory (1841–1862)