In Stephen King’s novel Tony is Danny's subconscious mind protecting him just like any other person's subconscious mind would do. Danny's difference, from most of us on this site, is in his ability to "Shine".
In the novel Tony is also Danny’s imaginary friend and he can talk to him face to face, but Stanley Kubrick reverses this in the movie and we don’t see him because he’s invisable. We just hear him talking through Danny in a strange voice. Another thing Stanley Kubrick changed in the movie is that other people can “Shine” and they also have invisible imaginary friends. Now how on earth could he show us an invisible entity on a movie screen? Just click here and find out.