On the way back; during the return trip (used to indicate doing something while returning).
帰りがけにコンビニに寄って牛乳を買った。
On the way back I stopped by a convenience store and bought milk.
帰りがけに友達とばったり会った。
I ran into a friend on my way home.
帰りがけ先生に質問しておいたほうがいいよ。
You should ask the teacher on your way back.
Just about to go back; at the moment of leaving (on the verge of returning).
帰りがけだったので、長居はできなかった。
Since I was about to leave, I couldn't stay long.
帰りがけに雨が降り出して、傘を取りに戻った。
Just as I was about to leave, it began to rain, so I went back to get an umbrella.