Barack was doing what he and many of the rest of us were taught to do when we were growing up. Some are still taught this.
This is from the official Boy Scouts website:
http://www.scouting.org/cubscouts/about/ideals/promise.html
...And my country: Duty to country starts with being a good citizen. This means caring about the people in our communities and helping those in need. Good citizenship also means obeying the law and using our country's resources carefully.
We show love for our country by respecting and saluting the U.S. flag and standing at attention when our national anthem is played.
The Scouts, a patriotic organization if there ever was one, tells their members to stand at attention when the national anthem is played. Nothing about putting one's hand over one's heart. Obama's grandfather served in WWII and so did my father. When I was little, I was taught that boys saluted and girls put their hand over their heart during the Pledge. Everyone stood 'attentively' (facing the flag, no talking) during the national anthem. My dad, being a veteran, saluted the flag as it passed by during the Fourth of July parade. My little brother imitated him even though he wasn't a veteran.
Customs vary based on what part of the country one is in, your age, your teachers' ages, your relatives' ages, and lots of other things.