Think like this. 계세요 is the honorific form of 있어요. It means stay, exist, be in a place, and has a few usages that aren't pertinent to this discussion. You use 안녕히 계세요 when parting company with someone when that person isn't going anywhere. This would include situations where you are leaving their house and they are staying, when you're completing a Skype call with that person, or when you expect the person is reading what you are writing.
Use 안녕히 가세요, or some version of that when the person is going somewhere as you part company. 가세요 is the honorific version of the word which means "go."
So the simplest way to think, if the person is definitely going, use 가세요. If not, use 계세요.