If you come to Mexico on a tourist visa, you can normally stay in Mexico for 180 days. That said, immigration officials at the point of entry will sometimes give you less. For example, my daughter came to Mexico to visit for the summer and she was only given 90 days, probably because as a teenager they considered that she does not have the means to support herself longer than that. So it is important to make sure you look at the time limit that is written on your tourist visit, just in case.
Before your tourist visa expires, you have to leave Mexico – but you can easily return right away on a tourist visa. So the short answer to the question of how long you can be in Mexico before having to apply for a residency visa is “indefinitely.” You will find that a lot of people never do apply for residency even though they basically live in Mexico. They just regularly leave every six months to renew their tourist visa. I guess people have different reasons for taking this route - one of which might be that they don´t have the financial resources required to meet the requirements for residency if they aren´t working. Other people are just not interested in going through the bureaucratic process, perhaps because they aren´t planning to stay here forever. However, it is difficult to live in Mexico without residency if you want to do things like open a bank account, work, qualify for medical care, etc.
(Urban lake in the upscale neighborhood of Polanco, Mexico City, Mexico, pictured.)