How it is calculated
Every file has three sets of permissions: for its owner, for its group and for everyone else. Each set has three bits, read (4), write (2) and execute (1), and one octal digit is their sum.
digit = read 4 + write 2 + execute 1 mode = owner, group, others 755 = rwx (4+2+1) r-x (4+1) r-x (4+1) 644 = rw- (4+2) r-- (4) r-- (4)
A fourth digit in front holds the special bits: setuid 4, setgid 2 and sticky 1. ls shows them in the execute places, s for setuid or setgid and t for sticky, in capitals when the execute bit underneath is off. On a folder, execute is the right to enter it, so folders need x where plain files do not.
Example
chmod 640 wp-config.php: the owner reads and writes (6 = 4 + 2), the group reads (4) and others get nothing (0). ls -l then shows -rw-r-----, and the web server can read the file only if it is in the file's group.
Questions
What does chmod 755 mean?
The owner may read, write and execute (7), and the group and everyone else may read and execute (5 each). ls -l shows it as rwxr-xr-x. It is the usual mode for folders and for scripts and programs others need to run.
What is the difference between 644 and 755?
Only the execute bit. 644 (rw-r--r--) lets the owner write and everyone read; 755 also lets everyone execute. Plain files such as HTML, images and PHP files need 644, folders and scripts that run on their own need 755.
Why should I not use chmod 777?
777 lets every user and process on the server change the file. If one site or plugin is hacked, the attacker can rewrite everything that is 777. A permission error is better fixed by giving the file to the right user with chown, or by adding the web server to the file's group.
How do I change permissions for all files in a folder?
chmod -R 755 folder changes everything inside, files included, which makes every file executable. To set folders and files apart, use find: find folder -type d -exec chmod 755 {} + for folders and find folder -type f -exec chmod 644 {} + for files.