I understand the mastery and how it works, but I can't find any mention of how long you have after an auto attack to get the effect on an ability.
It's 5 seconds.
From the wiki:
Damaging an enemy champion with an auto attack increases ability damage by 1%, stacking up to 3 times (max 3% damage increase) for 5 seconds.
So after you auto attack you will have 5 seconds to hit them with an ability to get the extra damage. Hitting them with another auto attack will reset the timer to 5 seconds, even if you're already at 3 stacks.