It depends on the mortgage.
Fannie Mae & Freddie Mac's waiting period is 4 years from discharge or dismissal date
FHA & VA - 2 years from discharge date
FHA Back to Work - 640+ credit score and 12 months of re-established credit with no late payments on rent
USDA - 3 years from discharge date
...waiting periods are subject to change and non-government sponsored loans will be different.