If primary cosmic rays are made mostly of protons, where are the electrons lost, and does this mean that the Earth is positively charged? Does the sun eject protons and electrons in equal number?
Great question. The electric field creates such a strong force that it would be very hard to move large amounts of just one type of charge. So astrophysical systems do generally eject equal numbers of protons and electrons. In particular, the solar wind is electrically neutral. So these cosmic rays are created in very nearly equal numbers, but by the time they get to Earth, there are far fewer electrons.
The main interaction that causes these electrons to get "lost" is inverse Compton scattering. What happens is that the electrons traveling between galaxies interact with the cosmic microwave background and lose most of their energy -- basically they bounce off of photons. This is where most of the electrons in cosmic rays are lost, which means that all the cosmic-ray electrons at earth will come from inside our galaxy. Of course, the scattering happens for protons and heavier ions, too, but it's far weaker for them. (The rate of energy loss is inversely proportional to mass, and protons and other ions are far heavier than electrons.)
As for net charges, there are still lots of open questions. But basically, if we accumulate much charge, we will soon attract a lot of the opposite type of charge, and become neutral again. So we don't think the Earth has much of a net charge, though I don't know of any precise limits.
Cosmic rays at the highest energies are likely protons or iron nuclei or something in between (Auger and High-Res continue to disagree on this point), but as AMS-02 has pointed out, there are lots of electrons and positrons. Since the energy of the incoming cosmic rays is irrelevant we have to consider them at all energies, including very low energies, at which point it becomes very difficult to measure rates.
I will answer the question in the title:
What happens to the electron companions of cosmic ray protons?
They leave them behind in the plasma on which they originated.
Have a look
Plasma is the fourth state of matter, it is composed of ions and electrons, and is continually ejected by the sun. Any positive excess on the upper atmosphere will be easily neutralized by absorbing electrons from the plasma.