If you are an undocumented immigrant in the United States (sometimes referred to as an "illegal alien"), 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. lawful permanent residence) is, however, another matter. In theory, these immigrants are "immediate relatives," and are eligible for a green card through the marriage. But undocumented immigrants face some high hurdles in claiming this right.
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 the non-citizen spent without legal documents in the United States, particularly if the person entered without inspection (for example, by crossing the border illegally).
If you have spent more than six months (180 days) 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 could be penalized for your illegal entry and stay.
The penalty for an unlawful stay of between 180 and 365 days is having to spend three years outside the United States before returning. If you have spent more than one year in the U.S. unlawfully, you'll need to spend ten years outside the United States before returning.
You might be able to get a "waiver" (legal forgiveness) of your illegal stay before leaving for the consulate, but these are hard to get. (And illegal entrants for the most part have no right to "adjust status," that is, file all green card paperwork and attend the 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 Adjustment of Status to Permanent Resident - FAQ.)
Immigrants who entered the United States (on their most recent entry) with a visa or other valid paperwork and are married to a U.S. citizen 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.)
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" marriage (not just an arrangement 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.
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.