Students have been giving teachers apples for hundreds of years. There are several different religious and nonreligious stories to explain this tradition.
The biblical book of Genesis tells the story of Adam and Eve. In the story, Eve eats an apple from the tree of knowledge. As a result of this reference, the apple has become a symbol of knowledge, and children give teachers apples to thank them for knowledge.
From 1700 to 1900, children in Denmark and Sweden gave teachers apples because they came from poor farming families that were unable to pay for school. Children also gave teachers apples because teachers didn't make very much money and families wanted to provide them with extra food.
Not everyone agrees with the Bible-based theory about apple-giving coming from the Genesis story. And certainly, school costs have gone down and teacher's salaries have gone up over the years. These days, children continue to give teachers apples simply as a tradition.