The present study tested the longitudinal (3 waves) relations among children's advanced theory of mind (AToM), emotional closeness with the teacher, and social functioning at school (loneliness and social skills). Seven hundred and fifty-one typically developing Polish children (344 boys and 416 girls, M age = 7.45 years (SD = 0.50 years) at T1) took part in the study. To measure AToM we used 17 tasks capturing children's understanding of second-, third- and fourth-order false belief, emotions and contradictory emotions, irony, pretence, diverse perspectives, faux pas, metaphors and persuasion; these tasks originated in a variety of publications. To measure closeness with the teachers we used the short form of the Student-Teacher Relationship Scale (Pianta, 2001). Social skills were measured by teachers with the social skills subscale from the Social Skills Improvement System Rating Scale (Gresham & Elliott, 2008). To assess children's sense of loneliness, we asked children to fill in the Loneliness and Social Dissatisfaction questionnaire (Cassidy & Asher, 1992). It has been demonstrated that (1) children who had AToM at T1 were able to establish closer, more positive relations with their teacher later on (T2) that were beneficial for children's functioning; (2) closeness with the teacher (T2) was found to mediate the relations between previous levels of AToM (T1) and later social functioning of children (T3); (3) AToM abilities (T1) reduced children's levels of dissatisfaction with peer relationships (T3). Overall, the study emphasises the importance of positive contact between children and their teachers for children's social functioning at school.