Great question. Modern encryption schemes are usually composed of a handful of primitives. If we consider something like HTTPS it uses both asymmetric and symmetric parts.
Asymmetric encryption is the "magic" that you are missing. Asymmetric encryption algorithms create a keypair, one half of this is the "public key" which can be used to encrypt messages that can only be decrypted by the "private key". This means that even if the public key is public (as the name suggests) the messages can't be decrypted without the public key.
You can think of this as giving someone an open padlock. They can put something inside a box and lock it using the padlock, but they can't open it without your key.
Using this you could come up with a very simple protocol for establishing a secure channel:
- The server sends you their public key, along with a certificate that proves that it belongs to them.
- The client then uses this public key to encrypt a key for symmetric encryption.
- The client sends this encrypted key to the server.
- The server decrypts the key.
- Now the client and server can both use the shared key to communicate.
(Note: There are many missing features from this system, but I think it illustrates the point. Don't design your own crypto.)