Usually, it's because they have something the other person wants, like if Iraq has oil and the US wants it, and Iraq wants guns and the US has that, then the two countries could form an alliance based off of trade, and the alliance could only prosper if one side has what the other side wants. Also, if a side needed help with a war, they could ask countries for help, and usually they have to give something in return, like if Guyana needed help with fighting with Kirghizistan, and they reached out to the US, the US could help them, but after the war, Guyana had to provide the US with titanium (These are examples, not actual things.). Hope this helps!