From the Super Mario Wiki
Assist Blocks are golden blocks that appear in the Nintendo 3DS games Super Mario 3D Land and New Super Mario Bros. 2. There are two types of Assist Blocks: one that contains an Invincibility Leaf, and one that contains a P-Wing, each shown with an icon of their respective power-ups. Each block is meant to contain these power-ups which assist the player to reach the end, and they appear near the beginning of the stage after losing a certain number of lives in that stage.
The Invincibility Leaf Assist Block only appears after Mario loses five lives in a single stage. Once Mario collects the Invincibility Leaf, he transforms into White Tanooki Mario (Super Mario 3D Land) or White Raccoon Mario (New Super Mario Bros. 2) and becomes invincible for the entire stage. However, Luigi cannot obtain one unless the player, as Mario, skips a world by Warp Zone.
In Super Mario 3D Land, the P-Wing Assist Block appears after Mario loses ten lives in a stage. Using the P-Wing takes Mario all the way to the end of the stage, in front of the Flagpole.
Assist Blocks don't appear in the Special Worlds. If Mario beats a level in Super Mario 3D Land, the Assist Block will not appear in that level. However, in New Super Mario Bros. 2, even if Mario beats the level without unlocking the Assist Block, he can still go back into the level and lose lives until it appears.
However, if any of the Assist Blocks appear in a stage, the player will be unable to have glittering stars on their save file (The same thing applies to the Super Guide Blocks from New Super Mario Bros. Wii). However, the player can get the glittering stars back by clearing the stage without using the Assist Block, after which the block will disappear.