If you are a foreign citizen who is in the United States without permission, having overstayed your visa, you can indeed cure the problem if you enter into a bona fide (real) marriage with a U.S. citizen and then apply for adjustment of status (a green card).
There are, however three cautions you should be aware of:
What type of visa did you use to enter the United States, and how long ago did you get that visa? If, for example, you got a student or H visa several years ago and met your spouse while in the United States, you should be fine -- no one is likely to accuse you of misusing the visa for a purpose other than its intended one.
But what if, for example, you have been dating your new spouse for quite some time, and a few months ago picked up a tourist visa, bought a wedding ring, and then arrived in the U.S. in order to get married and apply for a green card? That, according to the immigration authorities, would be a misuse of the tourist visa. You weren't planning to be a mere tourist, you were planning to immigrate to the United States. You could be denied the green card on that basis, though there is a waiver you can apply for. Talk to an attorney for a full evaluation of your case, and about the possibilities of applying for a waiver.
A foreign citizen who entered the United States with permission (on a visa, visa waiver, or something similar), then marries a U.S. citizen, may file for a green card using the procedure known as adjustment of status, based on his or her immediate relative relationship with the U.S. citizen. It doesn't matter, in this case, that you overstayed the visa. If you can manage to avoid getting picked up by the immigration authorities until the day you submit your adjustment of status application, then your subsequent stay in the United States will be lawful, and you'll be able to get your green card here (unless there are other problems with your application or eligibility).
Note that if the foreign citizen entered the U.S. illegally, however, he or she is not usually permitted to file for adjustment of status.
What's so special about adjusting status? It protects you from the three- and ten-year inadmissibility bars that are applied to people who have stayed in the U.S. unlawfully for 180 days or more and then left.
If you were to leave the U.S. after an unlawful stay of 180 days or more, and then attempt to apply for your green card through a U.S. consulate, you might be asked for proof of your location after the time your U.S. visa ran out. For details on how consular processing works, see "What Happens During Consular Processing?"
If the consulate becomes aware of your unlawful stay, you could possibly apply for a waiver (legal forgiveness), but you'd have to show that refusing to admit you as a U.S. resident would result in extreme hardship to your U.S. citizen or lawfully resident spouse or parent. In order to prove extreme hardship, you must provide evidence of hardship with respect to factors such as health, finances, education, personal considerations, and the like. This can be difficult to prove.
See our article on getting married abroad vs. in the U.S. to learn more about the differences.
There's a major exception to the rule allowing immediate relatives of U.S. citizens who entered the U.S. legally to apply to adjust status in the United States. It doesn't work if your entry was on a K-1 fiance visa, but you didn't marry the fiance who petitioned you. Marriage to a different U.S. citizen may still allow you to get a green card, but you'll have to leave the U.S. first. The citizen will then have to file an I-130 visa petition on your behalf, and you'll need to do your application and interview through a U.S. consulate in your home country. Watch out for the inadmissibility bars if you stayed unlawfully in the U.S. for six months or more.
An experienced immigration attorney can help you confirm your eligibility to adjust status, prepare the paperwork, and otherwise advise you on the best strategy for obtaining a U.S. green card.