Among dairy foods, butter is perhaps the most robust. It can be stored at room temperature longer than milk and most cheeses, for example. Butter contains a low percentage of free water and a high percentage of fat, which makes it unfavorable to the typical bacteria and molds which thrive in other foods. Butter is not immune to all organisms which comprise food hazards to humans, and it can break down chemically over time. Here are the details.
A frequent problem with butter is that its fats can take oxygen from the air and form fatty acids, which have an unpleasant smell and taste, along with a different color.
Rancid butter does not pose a health risk, if consumed in small quantities. Don't use a lot of it on your bread, or for cooking, as it contains corrosive molecules called free radicals, which are linked to cancer and accelerated aging in mammals.
The organisms we refer to as "molds" are fungi - not very different from edible mushrooms and fungi used in salads or on a steak, but much smaller. Molds are microscopic. They need oxygen, moisture, and organic matter to live.
Because butter is so high in fat, low in water content, and not very high in organic matter favorable to molds, the ones which do grow on butter are fairly limited. The three common types of mold found to live on butter are:
Somewhat to my shock, I have found some food experts suggesting that moldy butter may be consumed if the moldy areas are trimmed off and discarded. I believe that is unsafe and suggest you not do it.
You can't tell how deep a mold's roots go into a food. Do not try to cut off the visible mold and eat the rest! You will eat mold which is below the surface, which may make you sick. Don't do it...
Mycotoxins are poisonous substances produced by molds. They can cause nausea and vomiting, headaches, gastrointestinal discomfort, and other problems.
Butter can be stored at room temperature for multiple days with no problems. Bacteria don't survive well in the fats, so they don't pose a food safety hazard with butter.
For storage longer than a few days, butter should be refrigerated to cold temperatures just above freezing - 33°F to 38°F (1°C to 4°C). It can stay fresh for weeks if chilled to just a bit above freezing.
Butter may be kept for weeks, or a month or two, if frozen.
As to containers, airtight ones are best. Many people simply keep it wrapped in its original foil or waxed paper. It is better to move the butter to a container with a top which can be pressed or screwed down to make a good seal.
The air where the butter is stored must be dry. It makes a real difference. My family doesn't use salted butter; in humid air, unsalted butter is more lkely to become moldy if it is kept for enough time.
All in all, butter can last a long time. Seal it in an airtight container, not its original wrapper. Keep it cold for longer freshness. If it does become moldy, discard it. Plan ahead so that you don't have to save too much butter at once. Just enough for a month is a smart and easy quantity to keep fresh.