I'm pretty sure the only gray area in this arrangement is "working in the US" while staying in another country on a tourist visa, but it's pretty ambiguous. I'm working for a US company and getting direct deposits to a US bank as a US citizen. If I am on a trip to another country and I sign into my company's VPN for a work-related video conference, and maybe fix some bugs, am I working illegally? Maybe, but immigration laws aren't really written with that scenario in mind.
This is a topic that frequently came up in conversations with ex-pats, since once you start doing this you'll end up meeting lots of other remote workers doing the same thing. On the one hand, you are injecting a lot of money from your home country into the local economy, which increases GDP and supports local businesses. On the other, you are using public services without paying taxes to the host country. In practice, you can't use services like universal healthcare or free education without being a legal resident, so it's debatable whether your presence is a net negative for the local economy. It's probably not legal, but like I said, the immigration laws don't take this scenario into account. I was forthright with all of the immigration officials I spoke to and all of them just kind of shrugged their shoulders and renewed my tourist visa anyway. I figured that some countries would start writing new legislation and creating new types of visas to take advantage of this scenario, and we can see this happening now.
If your question is "is this legal", the answer is "it's complicated, but probably not". If you're asking if it's possible to do this long term, the answer is yes, it's not only possible but pretty easy. I was able to do this for almost a decade with no issues whatsoever. I'm glad that some countries are starting to think about this, because I'd much rather live with confidence that my work situation is accounted for than have to wonder if my residence there is dependent on the interpretation of each individual in the immigration department.