The topic is Public Key Infrastructure (PKI), with private keys and public certificates, which allows two parties - traditionally called Alice and Bob - to encrypt their communications. For this purpose, standard protocols are designed: Transport Layer Security (TLS), formerly known as Secure Socket Layer (SSL), that we use daily in HTTPS connections of our browser (the little lock in the address bar). Not everyone will have to configure PKI daily, so how again does it work?
Secure communication involves a number of different things:
The first three goals can be achieved by encrypting your communication. For this, you need a secret key that only Alice and Bob know. Then the problem shifts to this: how can Alice and Bob safely exchange their key? Not over a communication channel: if Zorro is listening, then he can steal the key and decipher their later communication. And if that communication channel is safe enough to exchange a key, then they do not need encryption.
Public Key Infrastructure is a brilliant solution to these issues. It uses 2 keys: a private key and a public key. Bob generates the key pair and makes the public key available to anyone who wants to communicate with him. With that public key Alice can encrypt a message. However, the message can not be decrypted with that key. That is only possible with the private key, which Bob keeps secret: he is the only one who can read the encrypted messages.
That such a thing is possible is a mathematical miracle (several, actually). The first and most widely used method was published in 1977 by Rivest, Shamir, and Adleman (RSA). For this purpose, they use the product of 2 large prime numbers, from which the public and the private key are calculated. See Wikipedia. The code is hard to crack because it is difficult to factor a large number into primes. Probably every code may be cracked if the spy has enough encrypted data as example; and enough time, computer power, and smart people.
In practice, it's too cumbersome to encrypt all messages with this algorithm. In the TLS protocol when two computer systems set up a connection (handshake), they exchange a one-time session key that they will use for simple symmetric encryption / decryption of their messages. That session key is encrypted with the PKI algorithm so Bob can read it but Zorro can not.