Theory
There are two boxes relevant to this:
1. A box of fixed size - the magic box
2. A box or rectangle that "hugs" your units as closely as possible
Gatsu's tip is that you need to keep all the units you are selecting in your group to be close enough so that they fit within the first fixed-sized box. If you do so, your units will stay in formation, move in formation, and cast spells in formation.
The first imaginary box is the more important one because it defines if your units will behave in that special way. The second box lets you know where you can place a command. If you place a command outside of the second box, your units behave in specially, but if you place a command inside of the second box, you units will clump together.
So basically, the idea is to ensure your selected units within the first box, and also that any commands you issue are beyond the second box defined by your units if you want them to behave in this special way.
[EDIT: As pointed out correctly by Locked there is a fixed box size for all ground units, and one for all air units]
To find out the sizes, I figured that the lenght of the sides of the square must equal the maximum distance that 2 units in a line perpendicular the the horizontal such that these units will stay in formation. To test the staying in formation a move command or spell can be used.
Here's a few units showing the box size:
The sides of the boxes are around these in pixels:
Ground units: 195 [EDIT: Revised to be 195 from more tests]
Air units: 255
So what do all these boxes mean? Well if you keep your units within those sized boxes, then you'll be able to stay formation and cast spreaded spells
Example applications
-Mine laying in TvP
If you wanted to do what I did in this picture, you'd have to make the 12 vults "hug" each other very tightly before they would fit into the box, which is obviously a time waster in a real game. This is also why if you absently-mindedly issue a mine command with 12 vults, you're like to have vults just move to the area you wanted to mine but then sit there and stare at you dumb-founded.
The key idea behind vult mine laying is to select 3-4 vults to use the mine command, rather than using the mine command for all 12 vults. 3-4 vults are far more likely to be in the megical box than all 12, which is why selecting the vults in groups of 3-4 to mine is far more effective.
Note that you also want to select 3-4 vults that are close together obviously, and not ones furthest apart in your group. The way to mimic what is seen in the picture in a real game practically is to deal with something like 4 groups of 3 vults each, and issue a mine command for each group.
If you watch carefully in VODs, you'll see this in action (intentionally or not) as a player splits up all his vults into smaller groups, then these individual groups move in formation (because they're within the box) and lay their mines without troubles. When it is done fast it nearly looks like all the vults are laying mines at once. It all looks very slick when you get it working right
-Keeping science vessels spread out in TvZ
Note that when science vessels are overlapping, they tend to push each other apart and slowly get further away from each other, so in this picture as the vessels are nearly beyond the box, you should move them towards a slightly tighter group (by right clicking in the middle then pressing stop or move somewhere else straight after), and you should do this periodically whenever you see the vessels getting too far apart. So it's basically keeping the vessels far apart enough for easy selection (they'll drift apart anyway if they're too close and overlapping) but not too far to exceed the box.
-Spread storming with P
Notice in the spread storming the storms are spread out because all 4 templars are within the size of the 195 by 195 pixel box. This is a less useful application because it is generally more efficient to cast storms manually one by one. If you really don't have time in game then you might want to select like a group of 4 or so templars that look sorta close together and use a single storm command for them.
While trying these experiments out, I also found that if you have a speed shuttle or normal shuttle dropping a nat or main mineral line with 2 temps, the 2 templars dropped are well within the area of the box, so clicking approximately in the middle of the mineral field will give a good spreaded double simultaneous storm so it will own the scvs (where the cursor is placed in the image is where you would click for aiming your storm to give that effect):
You may however want to cast individual storms and cast them straight away and not wait for both templars to be dropped, it's up to you.
-Muta micro with Z
Sometimes you want your units to clump, and magical boxes can explain why you hotkey an overlord or larva or trapped ling with your 11 mutas to clump them. Because your mutas are so far away from your last unit that you have in your selection, your selection is constantly larger than the 255 by 255 air box, and hence every move command means your units will clump, which is usually good for muta harass. If for example you wanted them to be spread out (maybe fighting corsairs, but I dunno anything about pvz), just spread them out but not too far, and they will stay in that spread out formation as long as they don't drift too far apart.
-Purposely clumping with other units
Other units you may want to clump together are corsairs in PvZ. Same idea holds. You just need a unit that is far away enough (ie in your base will do) and can't get close to your corsairs, so its' stuck there. An example I saw Nony use in a PvZ was to trap a probe against minerals using a pylon, and select that probe with the rest of his corsairs.
One thing to note is that if you're gonna use a worker as your trapped unit, make sure you're aware of the fact that if you use another unit to trap your worker, and you right click onto a mineral patch somewhere on the map, your worker will slide through the unit that's blocking it and your thing will be broken. Nony's pylon is good because the probe can't move out of that spot even if a command was given to move onto minerals.
-Marine/medic micro in TvZ
Magic boxes can't be used in a simple manner with mm. The whole idea is to have your units spread out as much as possible. But having them so spread out immediately places them beyond the confines of the magic box, and so any move or attack command clumps them together. So either you go with the extreme and have your units in an arc as big as possible, or have them close enough that they fit into the magic box. The danger comes in arranging your units so that they are somewhere between these two cases. You won't get the benefits of a big wide arc, and you won't get the benefits of moving in formation with the magic box.
One possibility of thinking about it is that when you don't need mobility but a good spread, then form the biggest and widest arc you can. This often occurs when you are containing the zerg at his natural and he's trying to push out with lurkers. When you need good mobility, keep your units fairly close so they fit within the magic box (but not really close together obviously). An example of this is when you are controlling units dropped from a dropship and you need to run around killing stuff. The lower number of units also helps you keep within the magic box.
Other stuff
Notice how the 2 templars are well beyond the confines of the 195 by 195 box as they are too far apart, so the storm clumps. Furthermore, if you do a move command on a unit or some other command like a spell or ability on a unit rather than an area of ground or air, it will clump up rather than spread.
[EDIT]
If the units are outside the confines of the box, it seems that using the minimap for the command does not help, and the spell or ability still clumps. I'm not sure where this notion comes from, but I believe its untrue. A possible reason is that any command that is issued by you but is focused on an enemy unit, for example 4 storms targeted on 1 unit, will cause the units who are given that command to clump. Using the minimap bypasses this because on the minimap you cant target 1 unit, however it does not ensure your units are within the box and as a result they might still clump up.
Also remember that if a command lands on a unit or building then it will clump no matter what, so make sure you give your command onto a patch of ground.
This "magical box" concept is not center of gravity technique nor does it have any relation to that technique (apart from the similarity in appearance between the spreaded storms and spreaded units which leads many people to link these 2 ideas together, when in reality they are not linked). Furthermore, many people will say to get your storms spread out like in the picture, just use the "center of gravity". This doesn't even make sense: what is the center of gravity in relation to 2 templars? The midpoint of the line that connects them? Storm at that point and kill your own templars? This concept of magical boxes ensures that the storm is always spread (if you do it correctly of course) without any "mumbo-jumbo".
[/EDIT]
And a nice video made by culture:
Hoped that helped ^^ And again big thanks to Gatsu for this tip.