Only citizens are allowed to vote in federal elections, so undocumented immigrants are not allowed to vote. Other people (felons, etc.) may also be prohibited from voting depending on state law.
The details of what happens depend on the state, but as a general thing:
In-person voting by someone identified as being on the list of eligible voters who haven't voted already: The fact that you voted will be recorded in a separate step from you actually voting. Your actual vote doesn't have your name on it so it is not possible to discard your ballot after it is cast.
In-person voting by someone not identified as being on the list of eligible voters: Even if you aren't on the list of eligible voters you can typically still cast a provisional ballot (this is federal law for federal elections). These ballots are kept separate with the identification of the voter attached to each one and they are only counted if the voter is found to be eligible. Otherwise they are discarded.
Mail voting if the outer envelope is intact: The ballot goes in an outer envelope that has your identification on it. Ballots are discarded if the voter is discovered to be ineligible (even if the voter was eligible when they mailed the ballot--there have been cases where the voter died between mailing the ballot and it being opened). This is one reason why some states only open mail ballots starting on the day of the election.
Mail voting after the outer envelope is removed: Usually there is an inner envelope which protects the privacy of the ballot. Once the inner envelope or ballot is separated from the outer envelope then the ballot no longer has your name on it and can not be discarded.
For all voting, if it is discovered that you cast or attempted to cast a vote while ineligible then you will be in serious legal trouble. Even if it was discovered before your vote was counted and instead your ballot was discarded. Even if you didn't realize that you were ineligible. For mail votes this generally applies to the time when you mailed it.