The current system (which is subject to change) is as such:
- For every Pokemon in a gym, it earns 1 coin every 10 minutes in the gym
- Coins are only distributed when the Pokemon is returned to the owner
- There is a daily cap of 50 coins, which resets at midnight for your local time.
If your Pokemon never gets kicked out, you'll never be able to "cash in" on any coins, regardless of how long it's been in a gym. However, as soon as it's kicked, it will earn you however many coins it has accrued, up to the 50 coin daily limit. This limit is across ALL your Pokemon. So you can have one Pokemon sitting for 8 hours and 20 minutes, or two Pokemon for 4 hours 10 minute each. As soon as they're kicked out, they'll grant you the max coins per day.