J’ai demandé à mon professeur la permission de sortir. I asked my teacher the permission to leave.
Je lui ai demandé la permission de sortir. I asked him the permission to leave.
“lui” is a personal pronoun that replaces “mon professeur” which has a function of an indirect object in this sentence. It answers the following question: “J’ai demandé à qui?” “ à mon professeur”.