The cleanup step is the second and final step of the ending phase. Spells and abilities may be played during this step only if the conditions for any state-based actions exist or if any abilities have triggered. In that case, those state-based actions are performed and/or those abilities go on the stack and the active player gets priority and players may cast spells and activate abilities. Once all players pass priority when the stack is empty, the step repeats.
From the Comprehensive Rules
(July 17, 2015
514. Cleanup Step
- 514.1. First, if the active player’s hand contains more cards than his or her maximum hand size (normally seven), he or she discards enough cards to reduce his or her hand size to that number. This turn-based action doesn’t use the stack.
- 514.2. Second, the following actions happen simultaneously: all damage marked on permanents (including phased-out permanents) is removed and all “until end of turn” and “this turn” effects end. This turn-based action doesn’t use the stack.
- 514.3. Normally, no player receives priority during the cleanup step, so no spells can be cast and no abilities can be activated. However, this rule is subject to the following exception:
- 514.3a At this point, the game checks to see if any state-based actions would be performed and/or any triggered abilities are waiting to be put onto the stack (including those that trigger “at the beginning of the next cleanup step”). If so, those state-based actions are performed, then those triggered abilities are put on the stack, then the active player gets priority. Players may cast spells and activate abilities. Once the stack is empty and all players pass in succession, another cleanup step begins.