There is a believe that if there is no one to listen our problem God will listen and everyone have a thinking that the temple is the place where one can search for a peace, happiness. And even God knows that his child will come and meet him.
And temple is the home for a god where he can live. Like in ancient time people thinks that if God arrives where he stays so something like that the temples were made for the god and there is a person who take care of his god.
Again if I talk about the purpose so there are so many purpose like someone is come for Only prayer means he is finding peace, someone come for giving happy news to his god, someone came to pray to God and ask him to help like buying something for his need. So someone came here for marriage.
If you know the rituals like chudakarana or mundan also done in temple because there is believe that the child come here from previous birth so that rituals is important and temple is the place where the god is take caring of his child..
So there are so many purpose of temples some people agreed some not..