Machine Code: How can an operating system prevent user-level programs from accessing each other's memory?

Ok, I did a lot of research on this, but I can not understand. The question is essentially: how can an operating system prevent a machine instruction from a user program (Firefox, Word, etc.) from accessing memory locations to which it is not supposed to to access?

This is what I understand until now: the machine code, as you will find for example in an .exe file after building your C / C ++ program, is a sequence of binary instructions. These instructions can be executed directly on the CPU (assuming that the program has been compiled for the instruction set of this CPU, for example x86). So, technically, you do not need an operating system to "run a program" in the broad sense – which is not surprising, because an operating system is just a program in itself . Moreover, the processor does not work in terms of "programs" or "processes", but in terms of individual instructions.

Now, if we have an operating system (Windows, for example), part of its job is to assign an encapsulated memory segment to each running user program (eg Firefox, Word, my own program C ++, …) and to make sure: that they can not read / corrupt the memory of each other.

It's here that things get confused with me. If I had to write my own operating system (or other nu-metal program), I should be able to use the machine's instructions to read / write from any location existing in the entire memory, is not it? So, how does an operating system ensure that no user-level program sends such instructions to the CPU, without "requesting OS permission" to access a certain location? memory?

I would like to know more about the subject, which goes from high level code to machine instructions. For example, I would also like to understand what program threads and user-level operating system processes look like, and so on. So, if someone could give me a good resource, that would be very appreciated. Nevertheless, this is not the heart of my post, just a side note.