So as you've probably guessed, a mulligan is never "legal" under the Rules of Golf. Mulligans are most often employed during friendly rounds by golf buddies; or during charity or playday tournaments where mulligans are sometimes sold. If mulligans are for sale, that means the golfer can buy, say, three mulligans for a set price each. The sale of mulligans is sometimes used as an additional fund-raiser at charitable events.
Are there rules governing the use of mulligans? No - whatever a group of golfers agrees upon is what counts (unless you are using mulligans in a tournament setting - then do what the tournament organizers tell you).
The most common mulligan customs, however, include using mulligans only on the first tee; and using one mulligan per nine holes. It's most common for mulligans to be used only off the tee; some groups might also allow mulligans from the fairway. It is rare for mulligans to be seen on the putting green.
As with all recreational golf customs, the use of mulligans varies from place to place. They are common in the United States, but rarely seen in the UK, for example. As with all localized golf customs, formats and betting games, clear up the ground rules before play starts to avoid possible confusion later.
There is also a playing format called Mulligans.
See also: Why are mulligans called that?