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. U.S. citizens marry illegal immigrants on a regular basis. The main limitations on marriage in the United States have to do with age and whether the person one wishes to marry is a close relative. In California, for example, you must be at least 18 years old to marry, unless you have parental consent.
Whether your marriage to a U.S. citizen will get you a green card (U.S. lawful permanent residence) is, however, another matter. In theory, immigrant spouses are "immediate relatives," and are eligible for a green card through the marriage just as soon as they can get through the application and interview process. But undocumented immigrants face high hurdles in claiming this right, particularly if they entered the United States without permission, as discussed below.
Despite the fact that your marriage to a U.S. citizen might be valid, when an illegal immigrant seeks to become a legal resident, various issues come up. The biggest concern is how much time the non-citizen spent without legal documents in the United States, particularly if they entered the country without permission or inspection (for example, they crossed the border illegally).
If you have spent more than 6 months (180 days) here after an illegal entry, and you go to a U.S. consulate in your home country for the final interview required to apply for a green card (as is required of most applicants), you could encounter problems. The consular officer could penalize you for your illegal U.S. stay.
The penalty for an unlawful stay of between 180 and 365 days is having to spend 3 years outside the United States before returning. If you have spent more than 1 year in the U.S. unlawfully, you'll need to spend 10 years outside the United States before returning. For details, see Understanding the Three and Ten-Year Bars for Unlawful Presence.
There are only two ways around this:
Of course, to get a green card based on marriage to a U.S. citizen, 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 a fraudulent, possibly paid arrangement to get you a green card).
You would 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 United States, and won't require need-based public assistance such as food stamps (sometimes called welfare). But as mentioned above, these are the least of your worries if you entered the United States without a visa.
In order to start the immigration process, the U.S. citizen would need to submit a petition on Form I-130 to U.S. Citizenship and Immigration Services (USCIS). That form asks for biographical information regarding both the U.S. petitioner and the immigrant, including current address. In past years, immigration enforcement authorities did not use this information to track down and arrest immigrants. But it has always been true that even if USCIS approves the I-130, the immigrant does not have any legal right to remain in the United States until their approval for a green card.
With vastly expanded immigration enforcement under the Trump Administration, however, there have been reports of immigrant arrests following upon the filing or approval of an I-130 petition. This can be especially problematic if you have any record of arrests or other issues that might make you deportable.
If you are 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 USCIS to ask for advice. Such a move could end up with you getting removed (deported) from the United States quite quickly, 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 an what risks you might face, assess your prospects of getting a waiver if you choose to go forward with an application that requires your being interviewed at a consulate abroad, and help you with the entire application process.