A defendant does not have to attend trial in a civil case. Your lawyers can run your defence without you. The court will make reasonable efforts to allow you to attend if you want to but if you can’t or won’t (for whatever reasons) the plaintiff has a right to their day in court without you.
In a criminal trial, you will be detained (by immigration authorities) until the trial is resolved. At the end of the trial (if acquitted) or sentence (if convicted) you will be deported. If the offence is relatively minor, the state may choose not to pursue them in order to expedite the deportation.