Managed code is not compiled to machine code but to an intermediate language which is interpreted and executed by some service(Common Language Runtime in case of .NET) on a machine and is therefore operating within a secure framework which handles dangerous things like memory and threads for us.
Unmanaged code is compiled to machine code and therefore executed by the Operating System directly.