From the Super Mario Wiki
Coin Snakes are types of moving Coins in Super Mario World and Super Mario World: Super Mario Advance 2. They are usually released from certain ? Blocks, and they move in the direction the player presses on the D-Pad (directional buttons) on the controller or Game Boy Advance. They are found in stages such as Donut Ghost House, Donut Secret 2, Valley Ghost House and Star World 5. Coin Snakes also appear in the World-e level Doors o' Plenty from Super Mario Advance 4: Super Mario Bros. 3, where they serve the same purpose.
If Mario or Luigi hits a P-Switch, the coins will become solid blocks, similar to Snake Blocks, which Mario or Luigi can ride while still controlling the blocks. However, if the Coin Snake touches a wall or obstacle, it will stop moving and can no longer be controlled.