Understanding the Staged Dynamics of Transformers in Learning Latent Structure