Malicious programs or malware often infect a mobile device when you download applications from an untrusted source.
This is one of the reasons why you should only obtain applications or apps for your mobile device from a trusted source such as the Apple App Store and Google Play. The applications from these sources have a lower chance of being infected with malware because these companies regularly review the applications in their stores. They also apply standards that app builders must meet and maintain to make their apps available for downloads.
You should also refrain from jailbreaking an iPhone or rooting and Android phone as this could make it easier for malware to infect your device.