*******. SPOILERS AHEAD *******
The book explains it very clearly - the "Game" in the title, while referring to many things, mainly refers to the fact that Ender — while fighting real battles — thought he was merely fighting in a simulator game.
He was only told that he destroyed the Formics once he won the last battle.
Graff and Mazer explain it to him later in the book:
"Of course we tricked you into it. That's the whole point," said Graff. "It had to be a trick or you couldn't have done it. It's the bind we were in. We had to have a commander with so much empathy that he would think like the buggers, understand them and anticipate them. So much compassion that he could win the love of his underlings and work with them like a perfect machine, as perfect as the buggers. But somebody with that much compassion could never be the killer we needed. Could never go into battle willing to win at all costs. If you knew, you couldn't do it. If you were the kind of person who would do it even if you knew, you could never have understood the buggers well enough."
**"And it had to be a child, Ender," said Mazer. "You were faster than me. Better than me. I was too old and cautious. Any decent person who knows what warfare is can never go into battle with a whole heart. But you didn't know. We made sure you didn't know. You were reckless and brilliant and young. It's what you were born for."
As far as "his purpose was to destroy the Formics? What did he think was going to do" - as per the book, that is also wrong. Ender very explicitly said to Graph, after finding the truth:
** Ender grabbed Mazer's uniform and hung onto it, pulling him down so they were face to face. "I didn't want to kill them all. I didn't want to kill anybody! I'm not a killer! You didn't want me, you bastards, you wanted Peter, but you made me do it, you tricked me into it!" He was crying. He was out of control.