Walt seems to always put on his hat when he gets into the Heisenberg mind set/character.
Is this the hat's sole purpose or is there a deeper meaning/connection to something else?
One could say that his hat is Heisenberg.
The actual hat is known as a Pork-Pie Hat which grew into style around the 30's and 40's. Not exactly associated with major meth empires.
The relation of his hat and Heisenberg can be rather simple, or it can be rather complex, depending upon how much you want to read into it and how much analysis you associate with the show.
It can be as simple as Walt feels a lack of confidence or even a great feeling of guilt for his actions without his hat on or near. With it on, he can rest assured that anything he does or says as "Heisenberg" won't come back to bite him in a kind of karmic sense. That he is embracing Heisenberg in order to protect and provide for his family. This quote from 502 "Madrigal" pretty much sums up his rational thinking:
Showing how much Walt is willing to commit in order to do what he feels is the right thing to do.
To go out on a limb with a crazy theory, it can be argued that the hat is the dead spirit of some kind that posesses Walt and uses his body to commit horrible acts of murder and evil planning. It has grown in power so much that Walt only needs to be near the hat in order to be taken over by the spirit within.
This theory requires more rooting out, and possibly needs the end of the series in order to flesh out to something believable but an interesting thought nonetheless.
A final thought to end this answer, it is possible that Walt is actually suffering from a mental disorder related to his cancer. It is possible that it has already came back, but not where the doctor's are looking and spread to his brain, similar to Jesse's theory in 310 "Fly".
However being familiar with how cancer scans work in general, and with the apparent ability of his doctor, I doubt this to be true.
I've always thought the hat was a vehicle for Walt to feel more confident in his ability to perform such actions.
From the beginning, we're shown that Walt is always the bridesmaid and never the bride (in reference to things like how he's second banana to Elliot Schwartz in Grey Matter Technologies and that this is a guy who's won a Nobel Prize and is now a chemistry teacher at a high school). From the jump we are introduced to a man that has little to no confidence in his abilities, and now he's diagnosed with cancer.
When he puts on the hat, we forget that. We don't see his bald head (the most vivid reminder of his cancer), so we don't see his weakness. All of the things that make Walt who he is are masked by the hat. That's why I think the hat makes him Heisenberg, because in that world Heisenberg doesn't have cancer, and cancer patients are always thought of as being so weak.
Walt lives a double life, and suffers from a split personality. I don't think it's a mental disorder but simply the fact that he had to behave differently in order to survive. After a while it becomes difficult to know which personality is the real Walt.
The hat becomes a marker as to which personality has taken over. When the hat is off we see the bald head of the pacifist, and when the hat is on it's clear we now see the antagonist. The hat is a non-ambiguous indicator as to which personality we are currently watching.
At the start of the show Walt has hair. The pacifist personality is dominate and he is diagnosed with cancer, but he still has hair. Walt gets the idea to cook meth and makes his first attempt at cooking. It's a complete failure. It's not until the birth of his antagonist personality does Walt shave his own hair off. It's an important scene because it marks the moment when his personalities split. Once his hair is off and he puts on the hat. Suddenly he's able to cook meth. He now has the two personalities and we will only know the difference when he's wearing the hat.