には can be used as a conjunction in order to mean “for the purpose of” or “In order to.”
“The best way to learn about Japan is to study Japanese.”
(lit. “For the sake of learning about Japan, studying Japanese is the best.”)
Otherwise, it usually is just a combination of に (marking direction/location) and は (contrast or making the topic). When being used this way, you can use it the same way you’d normally use に. For example:
“There was no one in the office.”
For your sentences, I don’t believe 私には友達がいます is correct, because you wouldn’t say 私に友達がいます. (Friends are in me?). I’m not entirely confident that it’s wrong, but it sounds a bit off to me.
Edit: It seems this kind of sentence is actually fine, my mistake.
However, you can use 私には in sentences like 私には理解ができません, which literally means something like “the ability to understand (that) is not within me.” Or 私には心がある. “There is a heart inside of me.”