Let's say heroin is legalized. People buy it, use it, some people OD and die. Oh darn. People die of alcohol poisoning too.
Some people manage their heroin usage responsibly and carry on with their lives. No problem. Some people can't control themselves and run their lives into the ground. No problem. Some of those people, though, take extreme (illegal) measures to get more, and that's where the trouble starts.
You have people prostituting themselves or ripping off convenience stores for more money to buy more heroin (or if those examples don't satisfy you, use your imagination). The legal dealers of heroin could also come under duress by those people irresponsibly managing their habits and pressed to extremes to support them. The problem with illegal dealing and "turf control" might be gone, but you still have people on the consumer side breaking laws to get their next fix.
Would legalizing drugs "solve" illegal immigration? No. It'd cut down smuggling and traffickers, who may make up a part of the population living here illegally. Illegal immigration is an international issue, and should be treated as such. You can only do so much to stop it if you only work within your own country, but to completely eliminate it, the governments of both, the countries being emigrated from and immigrated to, need to work together.