As long as your source of income is entirely outside the country, which it typically is, then in most cases you're fine. You're essentially just like a travelling investor. It's earning income from within the country that you can't do legally without a visa.
That's not true, and it's often not well known, but your own physical location is also a factor, not only your source of income. Most countries would never detect this anyways, but you shouldn't think that it's legal at all.
Well then I guess the lawyers I hired in country were all lying to me? As long as I didn't overstay my visa, didn't stay more than the required time for physical presence test tax liability (typically 180 days), and didn't work for firms inside the country, I was legit.
Please let me know if I was given bad legal advice, because I don't want to get thrown in jail in the future.
If they told you the legal advice they were giving you for a specific country was valid in all countries, and that all countries treated this income the same way, then yes, they lied to you.
This is not correct if you are doing actual work in the country. What's happening is, many of these countries are low income ones; and this kind of work is far removed from their economies. So they are turning a blind eye to it.