The deadline to do so in time for Election Day is 7 days before the Election if by mail, or by 3:00 p.m. on the day before the Election if in person at your County Clerk's office. Once you apply for a mail-in ballot, you won't be able to vote in person at your polling place (though you will still be able to submit a provisional ballot).
Ballots can be sent to you by mail or picked up by an "Authorized Messenger" (such as a family member; see application for more details) who will then bring it to you.
If you apply in person at the County Clerk's office and they give you the ballot then and there, you can, if you so choose, fill it out and give it right back to the County Clerk, who will make sure it gets to the office of the Superintendent of Elections/Commissioner of Registration and counted accordingly.
NEW IN 2018:
If you received a mail-in ballot for the 2016 Presidential election, you will be automatically enrolled to receive one in the 2018 general election.