Towards Revealing the Mystery behind Chain of Thought: A Theoretical Perspective