If you are an undocumented immigrant in the United States (sometimes referred to as an "illegal immigrant"), nothing stops you from marrying a U.S. citizen, or most anyone else you wish to marry. U.S. citizens marry illegal immigrants on a regular basis. (The main limitations on marriage in the U.S. have to do with your age and whether the person you wish to marry is a close relative.)
Whether that marriage will get you a green card (U.S. permanent residence) however, is another matter. In theory, immigrants who marry U.S. citizens are "immediate relatives," and eligible for a green card. But undocumented immigrants face some very high hurdles in claiming this right.
Learn more about visas and green cards based on marriage to a U.S. citizen.
The Problem of Inadmissibility
Despite the fact that your marriage may be valid, when an illegal immigrant seeks to become a legal resident, various issues come up. The biggest concern is the time you have spent without legal documents in the United States, particularly if you entered without inspection (for example, by crossing the border illegally).
If you have spent more than six months here after an illegal entry, and apply for a green card, you will most likely have to travel to a consulate outside the U.S. for your green card interview. There, you will be penalized for your illegal entry and stay by having to spend three years outside the United States before returning. If you have spent more than one year here, you'll need to spend ten years outside the United States before returning. You may be able to get a "waiver" (legal forgiveness) of your illegal stay, but these are hard to get. The reason that all of this will happen at a U.S. consulate outside the United States is that, due to your illegal entry, you have no right to "adjust status" (file all your green card paperwork and attend your interview) within the United States. (There are a few exceptions, however, for people who fall under an old law called 245(i) -- to find out who can take advantage of these, see "Frequently Asked Questions About Adjustment of Status.")
Immigrants who entered the United States (on their most recent entry) with a visa or other valid paperwork don't, however, have this problem. That's because they are allowed to submit all their paperwork within the United States and attend their interview here -- which means they can't be penalized by being kept from returning to the United States, because they never left. (They may face a separate problem, however, if they used a visa meant for one purpose, such as tourism, with the secret idea of marrying a U.S. citizen and getting a green card. That's visa fraud, and can lead to green card denial.)
Meeting Other Criteria for Getting a Green Card Through Marriage to a U.S. Citizen
Of course, to get a green card based on marriage, you would also have to prove that you meet all the various other eligibility criteria, such as that it is a valid (legally recognized) marriage and that it's "bona fide" (you're not just getting married to get a green card).
You'd also have to overcome any other possible grounds of inadmissibility, by showing, for example, that you haven't committed certain types of crimes (or more than two of any crimes), don't have a communicable disease that would present a danger within the U.S., and won't require public assistance (sometimes called welfare). But as mentioned above, these are the least of your worries if you entered without a visa. See "Immigrant Inadmissibility to the U.S." for more information.
If your marriage is less than two years old at the time of your approval for a green card or entry to the U.S., click here to learn about conditional green cards.
Getting Legal Help
If you're an undocumented immigrant who wishes to obtain a green card based on marriage, you should absolutely consult with an immigration attorney before making any decisions. Whatever you do, don't walk into an office of U.S. Citizenship and Immigration Services (USCIS) to ask for advice. Such a move could end up with you getting removed (deported) from the United States, and prohibited from returning for many years. An attorney can help you evaluate your situation, determine whether you have a possible path to a green card, assess your prospects of getting a waiver if you choose to go forward with an application that requires your being interviewed abroad, and help you with the application process.
Learn more about family-based visas and green cards.