The way dogs get infected with heartworm disease is by being bitten by mosquitoes which causes the heartworms inside the dog.
So if the other dog doesn't get bitten by mosquitoes then they won't be infected with heartworms.
So no a dog with heartworm disease cannot infect another dog who doesn't have heartworms since heartworm disease is not contagious and only happens from mosquitoes.
If your dog gets bitten by mosquitoes then you should treat them for heartworms or take them to the vet because heartworm disease is deadly in dogs within a few weeks to a month or two months depending on how severe the heartworm infection is.
Some people give their dogs heartworm medicine as a preventative measure for the heartworms but it's really not needed and you should only need to give the dog heartworm medicine when they get bitten by mosquitoes or during mosquito season.
Giving your dog too much heartworm medicine can shorten your dogs lifespan but when you give the dog heartworm medicine when needed it's okay and the dog will usually live a good normal lifespan.