The truth is that nobody really knows who said that phrase first.
Niccolo Machiavelli is often quoted as saying the opposite - that the end justifies the means - but he never actually did (he gets mis-quoted a lot). The closest he ever comes is this:
"in the actions of all men...when there is no impartial arbiter, one must consider the final result."
Or in other words that if there is no other way to tell what is a good thing to do, you can at least try and judge by the outcome of events.
The reason we probably don't know who said things like these is that they are really very common ideas. People have been arguing about this kind of thing for just about as long as there have been arguments at all.
What your version of the expression means is that you CAN'T use a desired outcome to say that what you're doing is okay.
For example, we might agree that eliminating AIDS from the world might be a pretty good idea, but that is just the 'end'. If someone decides he's going to get rid of AIDS by murdering every man, woman, and child with the virus so it can't spread... well, most of us would say that was a VERY bad way of doing it (the means)!
In other words, it's not enough just to be trying to do good things... you have to also do them in a good manner!