I have read that there are some harmless bacteria live on skin and they also have role in protecting body against some harmful bacteria but how do they do so? I mean how these bacteria are able to resist other bacteria which are harmful?
Relations between organisms in complex ecosystems are various, so I won't give an exhaustive answer, only keys to understanding.
First, you need to consider that a bacterium harmful to humans is not necessary harmul to other bacteria. So there is no reason why our skin microbiota will only target/resist to human pathogens.
What's more, microbes are not inherently harmful to us. Their dangerosity also depends on the capacity of the skin to block them 1.
Then, microbial defence mechanisms depend on the kind of relation they have to the potential pathogen: competition, parasitism, predation, etc.
The article cited below will give you a nice overview on the topic.
1 Cogen, A. L.; Nizet, V.; Gallo, R. L. (2008) Skin microbiota: a source of disease or defence? British journal of dermatology, 158, 3, 442-455; doi: 10.1111/j.1365-2133.2008.08437.x