It is all about the frequency produced by the driver and how it interacts with the front baffle (the front panel on the speaker).
When the wavelength is equal or less than the cone of the driver, it will beam straight ahead and radiate very little.
As the wavelength increases, it "sticks" to the baffle and disperses in a semi-circular pattern.
When the wavelength becomes equal to the width of the baffle, it begins to diffract around the edge. This diffraction pattern is mostly radiated away from the listener.
As the wavelength increases, it will begin to radiate from the sides producing an odd lobed radiation pattern as is interacts with the front panel radiation.
When the speaker is too close to the wall, the diffraction from the baffle edge is reflected back at the listener and sounds terrible. This can be mitigated to a degree by rounding the corners of the speaker, but it does not cure the problem. Turning the speaker a bit moves the reflection away from the listener and can sometimes greatly improve the sound, but can also increase fatigue if it puts you in a high frequency beam.
Also, when the speaker is too close to the wall, the frequencies being radiated by the side reflects and interacts with the sound radiated from the front and causes combing, which also sounds terrible.
A 6" (15cm) driver will beam at 2200 Hz and above.
If the same driver is positioned in the middle of a 12" (30cm) baffle, it will have edge diffraction at 1100Hz, and will probably start having significant combing at about 800Hz and below.
Rear placement is room dependent.
A room "node" will occur at 1/2 the wavelength between the walls. Picture a guitar string being bent up, it is fixed at both ends, and it will swing up and down when released.
A 12' (3.6m) deep room will experience a "node" at 46Hz, and the room will resonate between the front and back walls at this frequency. Speakers at this frequency radiate in a spherical pattern, and will comb badly when they interact with the room node and it's harmonics which occur at each integer multiple (92Hz, 138Hz, 194Hz, etc). The only cure for this is moving the speaker into the room so it no longer interacts with the node. Room treatment helps, DSP makes a mess of it.