Before I left work, my ex boss invited me eat dinner with him. I will be leaving the country soon, and I am planning to invite him to dinner as a gesture of thanks and to say goodbye. However, he is much older than I am and is of higher position. Would it considered rude or unprofessional to invite him to dinner?
Dinner I usually reserve for people in my life whose company I enjoy. Dinner for people who I've already parted ways with is usually a pain. It's good that you're aware he is in a higher position and that this is relevant, and you are right. You are probably just not worth his time anymore and that is okay.
You may suggest you would be available for a coffee or drink (both of these are lower obligation, culturally speaking). Be prepared to be turned down, and, serious question: would this be enjoyable to you in the first place anyway? Do not use up time just in an attempt to show gratitude.
Lastly, if you do want to express gratitude, it would be appropriate to write a thank you note for his work mentoring you (like, a few sentences tops). Email is fine. A card or letter would be a bit dramatic.
Adjust any of this advice as needed to your local culture and working relationship. (But you wanted general advice and this is it).
No it wouldn't be considered rude or unprofessional at all. It seems like a very nice gesture.
It's a terrific way to say "thanks" if you really mean it.