Whether a headboard should be placed against a wall and whether or not to attach the headboard to the wall are questions which aren't as easily answered as one might think, because there are different types of beds and room layouts which might determine such things. Generally speaking, yes, the bed should be against a wall. Whether the headboard should attach to the wall depends on the type of headboard being used and whether it was designed as a wall mount headboard or part of the bed frame.
The Short Answer
Hotels tend to use headboards which are designed to be mounted on the wall, the bed frame holds the box springs and mattress and is pushed against the wall in line with the headboard so they appear to be a single unit although they are two separate pieces of furniture. There is nothing wrong with that design for use in a home, when there is clearly a place the bed should be located and never be moved. It becomes an issue when, over time, the bed placement needs to be adjusted. Perhaps a new piece of furniture requires the bed to be pushed a few inches over to maintain balance in the room, or over time the person using the bed realizes they could have a larger dressing area in the bedroom by sliding the bed a bit further toward the corner. That's not going to be possible when the headboard is permanently mounted on the wall.
Alternatively, a freestanding bed in which the headboard is attached to the bed frame is much easier to reposition according to the needs of the room over time. However, such a bed tends to become loose over time. The headboard can become shaky or squeaky, which might be uncomfortable for someone who likes to sit up and read or watch television in the comfort of the bed when they aren't sleeping, or if kids like to run in the room and jump on the bed in the morning, or during the more interesting activities which later lead to having children who come jump on the bed in the morning. Such headboards can be attached to the wall if necessary, but that's not how they were designed, the process is a bit more difficult than it might seem, and there are better solutions.
Bed Placement and Positioning
Choosing a headboard style, whether it is going to be wall mounted or frame mounted, sometimes is a matter of personal preference and in other situations is demanded by the layout of the room. Some bedrooms provide options as to where the bed should be positioned, others demand it be placed in a specific location. As a quick example, placing a bed in front of a window is usually a bad idea because blinds and curtains are going to permanently close off the window without easy access when the window needs to be opened for light and fresh air. However, some bedroom designs dictate the bed be placed under the window, in which case it's going to be centered to the window and never moved. Although knowing the bed is only going to be in one specific location is generally an ideal situation for a wall mounted headboard, it can't mount to a window, so that just won't work.
Other room layouts might allow for a wall mounted headboard, but such a headboard can potentially be problematic over time. A single person might want the bed pushed in a corner with just enough room on the unused side to change sheets. Such positioning allows more floor space for dressing, doing makeup, or morning stretches. Later as a relationship forms, the couple will likely want to move the bed further into the room so each person has easy access to their side of the bed. Nothing is wrong with either set of reasoning for how to position the bed, but a mounted headboard won't accommodate the change because it's not easily moved.
Reasons To Consider Attaching a Headboard to the Wall
Although there are disadvantages to a wall mounted headboard, there are situations when it really is a better idea than a bed frame mounted board. They can be really great in a small studio flat where the resident is away from home during the day and spends their time at home on the bed. The wall mounted headboard is secure and a permanent focal point for such a room. An apartment with thin walls might also be a good reason for the wall mounted headboard, because it won't squeak or knock against the wall to potentially disturb the neighbors. Preventing squeaking noises and knocking against the wall might also be a good idea for someone with restless leg syndrome or otherwise tosses and turns while sleeping.
Another consideration is how a frame mounted headboard can, theoretically, be permanently attached to the wall. The first thing people overlook when they consider such a solution is the baseboard at the bottom of the wall. To keep the headboard straight and level, either part of the baseboard has to be cut out and removed or board the same width as the baseboard has to be attached to the wall for the headboard to mount to. Although either of those options “will” work as a solution, neither is the ideal way to place and position the bed.
It's important to remember that the considerations toward choosing a headboard have been considered by millions of people as they buy furniture and decorate a new home. There are nuanced specialty items which solve specific difficulties a particular bed might present, but there are three general styles of headboard, any one of which might seem standard to any person depending on what they grew up with at their parents' home:
Headboards Attached to the Bed Frame
Attached to the bed frame headboards are ideal as freestanding furniture pieces which are relatively easy to move. They do become loose over time, but can be tightened back up if the bed becomes shaky or knocks against a wall. They can also turn into the traditional movie joke about a bed that squeaks when any movement occurs on it, but again, such a problem can be resolved by tightening the bolts which hold it together, or in some cases, properly adding a bit of wax or light oil can prevent the joints from making noises.
Wall Mounted Headboards
Wall mounted headboards are great as decorative items which aren't actually attached to the bed itself. Such a style allows for the simplicity of a free standing bed frame which holds the mattress, or the mattress and box springs according to design, without attached parts and joints which might require maintenance over time.
Freestanding headboards are a relatively new solution designers have found to solve the problem of a frame mounted headboard which might not be secure or a wall mounted headboard which can't be moved. In the most basic of terms, a freestanding headboard is a wall mounted headboard, but attached to a stand instead of the wall itself.
Everybody has different needs so there is no final answer to the question, “What is the best headboard?” What is consistent is that well made quality craftsmanship is always going to be a better piece of furniture than poorly made pieces which might look good in the store but neither look good nor perform their function well in the home. A great bed which lasts a lifetime is going to be worth the money it costs and the time it takes to make a final decision before deciding on a purchase.