Malicious code can infect a system through various means, including email attachments, infected software downloads, malicious websites, and social engineering techniques. Once the code is executed on a system, it can take various actions, such as stealing data, encrypting files, or spreading itself to other systems.