Modern CPUs have privilege modes that are used by the operating system lock out certain instructions. For example in user mode the instructions that modify (raise) the privilege mode or access system resources like the currently configured page tables cause exceptions. This allows the operating system to decide whether to abort your user process, or to emulate the operation.
Does the OS look into my code before loading it, and sees if I'm using there certain instructions that I'm not allowed to use and if so simply won't execute it?
No, it doesn't have to — the operating system puts the processor into "user" mode whenever it runs user code. That will activate the hardware's exception mechanism should a privileged instruction be encountered — in user mode, the exception happens instead of executing the privileged instruction (triggering an exception is the execution of that instruction rather than any privileged operation).
Btw, the system call types of instruction used by user mode code to request services from the operating system also activates the hardware exception mechanism.
How is it being managed?
The operating system always runs user code at user mode, aka user level privilege, and generally runs its own code at higher privilege. These modes inform the processor how to handle privileged instructions. Most user mode code won't even bother to try to execute privileged instructions as they are useless, but if they do, the hardware exception mechanism kicks in and effectively tells the operating system that this has happened and lets it handle the situation.
In order to run user mode code, the operating system might use a "return from interrupt" instruction, to restart the user code (whether it technically has or hasn't been previously started doesn't matter). A return from interrupt is a type of instruction that is one way to change the privilege level, while also changing the instruction stream (aka branching); such an instruction itself is privileged, meaning the processor won't allow it in user mode.
When the processor gets an interrupt, it notes some of the critical CPU state — the critical CPU state is that state that it necessarily has to modify to service an interrupt. Servicing an interrupt transfers control of the instruction stream feed into the CPU, by modifying the program counter aka instruction pointer; on interrupt, the processor effectively makes a sudden branch to the interrupt service routine. It also makes a sudden change of mode to higher privilege that allows the ISR access to more instructions. Because these two sudden changes are needed in order to activate the ISR, the hardware will record the prior values for software to use later when resuming the interrupted user mode code. Thus, the hardware & operating system conspire together to run the OS in at high privilege and the user code a low privilege.
When the user mode program uses a syscall type of instruction (requesting operating system services, like I/O), the same hardware exception mechanism transfers control to the ISR. When the operating system wants to resume the user mode process, depending on the hardware, it may have to manually advance the program counter of the user mode process across the syscall instruction before resuming — it is as if, to the user mode process, the operating system simulated/emulated the system call.