Why do viruses replicate?
The answer to "why" is something along the lines of they have to in order to survive. They're generally not considered to be "alive" by the traditional definition because they don't consume nutrients or breed, they use other cells to fulfill both those requirements, but they still need to breed to survive just like any living organism would.
"How" is probably a more useful answer. They have they're own RNA stored within their "bodies," which, once they attach to a living cell, they inject into the cell. The RNA makes its way into the nucleus and basically modifies the cell's genetic material to produce viral cells instead of living cell material.
So, for instance, instead of a cell using its energy to dispose of waste, grow itself, replicate itself, and process nutrients, it is basically hacked by the virus to grow more viruses inside the plasma membrane.
The inside of the cell fills up with viruses until it bursts, and then these viruses float around to perform the same process on other cells.
Viruses contain DNA or RNA. Viruses that contain RNA are called retroviruses, and the RNA undergoes reverse transcription to form a DNA molecule. Viral DNA gets incorporated into the host cell genome and takes over the cell to make it produce and assemble viral parts. Eventually so many new viruses are replicated that the cell bursts, resulting in viruses getting into the environment where they can infect other cells.