The most important thing you need to keep in mind is that no one in the gym cares about what you look like, how much you weight, and what exercises you perform.
Having said that, a gym is a common area, so it pays to be considerate. While exercising, observe the room. See how people wait for machines to be ready, how they behave, etc. Look for signs that talk about rules (for example, some gyms limit cardio machine usage ). If you're unsure about something, ask. Unless they're in the middle of their workout, most people are more than happy to help. The gym staff are there to help, so always feel free to ask them as well.
Weights: if you're weightlifting, you're going to rest between sets. If the gym is busy, it is good form to share: other people can use the machines/devices/weights while you're resting between sets. Racks (squat, bench) get busy and sharing helps everyone move more quickly. Quick note: if you're sharing equipment/machines, and you're listening to music, male sure you take your headphones/earphones off so you can communicate. You need to hear your buddy. This is especially important if you're ever spotting someone.
If you're using a bench or a seat, it's good form to place a towel where you might sweat on it, and wipe it down after you're done. Before grabbing weights from a machine/bar, check to see if someone is still using them (asking won't harm). Always return your weights to the weight rack. Don't be the disruptive asshole (grunting loudly, dropping weights). If you're unsure about something, or need help with an exercise, ask other people who are not actually working out. The big buff guys are probably the nicest as they usually remember what it was like being a beginner.
Some seasoned lifters in here pointed out that dropping weights and grunting are part of the workout. That's very true, but doesn't mean you get to be an asshole about it. Is there rubber on the floor? Is there a deadlift area? Are you grunting in effort or to show off? Again, it's really about being considerate.
Cardio: observe the time limit if there are people waiting, and wipe your sweat off when you're done.
Studio use: return everything you use in there. Turn off the lights if necessary. Don't drop weights on the wooden floor.
Body weight area: make sure there isn't a class or someone else using the equipment. If it's an individual, ask if sharing is ok. If it's a class, either join in/sign up, or wait for it to finish.
Group classes: these usually need you sign up early, and your membership needs to include them. Always put your equipment away after the class is done, or wipe your sweat puddle. Think of people using the gym after you.