The answer is:
The end for which an act is undertaken is one intended by the agent. What an agent intends is of two kinds.
Sometimes he performs an act so that his own action will bring about what he intends. He would not undertake such an act knowing that its intended outcome would not occur. Allah Most High does whatever He wills: whatever He wills comes to pass, and whatever He does not will does not come to pass. But Allah does whatever He wills.
At other times, he wants someone else to perform an act of his own choice, so that the person acting may benefit from his action, while that action is something the first agent loves. This is like someone who builds a mosque so that people may pray in it, gives them money with which to perform Hajj and wage jihad, gives them weapons with which to wage jihad, commands them to do what is right so that they may do it, and forbids them to do what is wrong so that they may refrain from it. If they do what he intended for them and wanted from them, it is good for them and something he loves. If they do not, this is not good for them, nor have they brought about what he loves.
Now, this person may lack the power to bring about their voluntary performance of what they have been commanded to do. This is why the negationist Qadariyyah claimed that the Lord has no power to guide His servants—a claim that the people of the Sunnah hold to be mistaken. Yet the one issuing the command may have that power, for had He, glorified be He, willed, He would have given every soul its guidance:
“Had your Lord willed, all those on earth would have believed, every one of them.”
But a created being may help some of those whom he has commanded because helping them serves a benefit for him, while not helping another. Likewise, the Lord Most High may help the believers, so that they do what they have been commanded to do and what Allah loves from them, while not helping others, owing to His wisdom in this. For an act can occur only when all that it requires is present and whatever is incompatible with it is absent.