Some occasions call for you to be wearing your heart on you sleeve and for other occasions you need to clam up.
The heart is often used as a symbol of ones feelings of love and passion. To fully display the heart in a conspicuous place like the sleeve is to make ones feelings clearly visible. Why the sleeve is chosen as the place to display the emotions is unclear.
The phrase is used in Shakespeare's Othello, in a line spoken by Iago: I will wear my heart upon my sleeve (1.1.65). Iago's plan, in context, is to feign openness and vulnerability in order to gain the trust of Othello, psychologically a powerful move on Iago's part, then use this trust to destroy Othello.
Iago's usage is consistent with the modern usage, except that Iago's intent was to deceive.
Alternative: This phrase originating from the middle ages. When a king's court would hold a jousting match, and a knight was dedicating his performance that day to a woman in the court, he would be given her colors, or a kerchief or something to tie around his arm, to show he was representing her.
Hence the term wearing one's heart on one's sleeve, because knights would joust to defend the honor of a woman they loved or cared for.