Water has three atoms per molecule and two of them are hydrogen, so you might guess hydrogen is two-thirds of water. By mass it is about 11%. The reason is mass per atom: an oxygen atom has a mass of 16.00 amu and a hydrogen atom only 1.008 amu. One molecule has a mass of 2(1.008)+16.00=18.02 amu, of which hydrogen supplies 2.016, so hydrogen is 2.016÷18.02=11.2% of the mass and oxygen is the other 88.8%.
That is the whole method: find each element's total contribution (atomic mass times number of atoms), then divide by the molar mass. Because you are splitting one total into shares, the shares always add to 100%, which gives you a built-in check. The answer also does not depend on sample size: a drop of water and a lake are both 11.2% hydrogen by mass, because every molecule has the same makeup.
Counting atoms is a different question. In glucose, C6H12O6, hydrogen makes up half of all the atoms (12 of 24) but only 6.7% of the mass. If a problem asks what fraction of the atoms are one kind, use the subscripts directly; if it says "by mass", weigh each element with its atomic mass first.
Think of it like this. Percent composition is like asking how much of a fruit basket's weight is melon. Two apples and one melon is mostly apple by count but mostly melon by weight, just as water is mostly hydrogen by atom count and mostly oxygen by mass. A basket's contents can vary; a compound's mass shares never do.
Core idea. Every compound has a fixed mass recipe: each element's share is its atomic mass times its atom count, divided by the molar mass. The shares add to 100% and are the same for any sample size.