This is one of the murkiest areas in immigration law, because most visa rules were written before remote work existed. Technically, many countries define work by where you are physically sitting, not where the client sits, so freelancing on a tourist or dependent visa can violate the letter of the law even when all income is foreign.
In practice, enforcement against short-term visitors doing their overseas job is rare, but the risk grows with longer stays, local clients, or anything resembling participation in the local labor market. Getting paid by a company registered in the country you are visiting is much more clearly prohibited without work rights.
The cleaner route is a digital nomad or freelancer visa, which many countries now offer to legalize exactly this arrangement and clarify the tax side. If you plan to stay more than a couple of months while earning online, check whether your destination has one, and remember that tax residency runs on a separate track from visa status.