One of the key elements here is the concept of public and private key pairs. The idea being that each computer, or In some cases, computer system, has its own set of two keys: each one of which is effectively worthless without the other. There is a public key, which is sent to everyone in the web server's certificate, and the private key which is only stored by the web server itself. Thus, your computer has the public key of the server and can then use it.
A document or a set of web communications (like exchanging a password), can be encoded with the public key of the server. Once done, no one without the corresponding private key can read that file. Since only the web server has that, only it can read your password.
In reference to the comments below, to clarify, here is a highly simplified version of the process:
- Your browser gets the public key from the web server when it sends you its certificate.
- Your browser then generates a new encryption key that is used for future communications with the web server. (This different key is called a symmetrical key and is used instead of the public private key pairs due to the computational horsepower that is necessary to use the public private key encryption.)
- Your browser sends that new symmetrical key to the server after encrypting it with the server's public key.
- The server and your browser then use that new symmetrical key to send and receive data including your password.
In addition, your web browser COULD have its own certificate: enabling your own browser to have its own private and public key. This can provide additional security to the server especially in ascertaining that you're who you say you are. However, as mentioned below, this is not common.
Boom. Two way, more secure communications. Nothing's perfect, but public key cryptography is a great step in ensuring privacy and security from digital crime.