In Civ 5, how exactly do agreements affect relations with other civs?

I'm currently playing my first successful game on immortal. The most important thing to keep the AI from attacking you is to have a strong enough military that you aren't an easy target. This means that you might have to forego wonders and other early buildings to catch up to the AIs starting units. You should also focus on scouting early, so you can meet as many city-states as possible, find some ruins, and kill some barbarians, all of which boost your economy. In my experience, it also means that you're going to have to attack someone early on in order to catch up to everyone else's growth. A few good puppet cities (preferably with one of those wonders you skipped) will put you back on par with the other AIs.

Some other tips regarding relations I've learned

  • Try to get a pact of cooperation early on, while there's still plenty of space between you and the other civ. They aren't time limited, so it can help protect you for a long time.
  • I've been turning down pact of secrecy offers, they limit my ability to be friends with as many people as possible.
  • I've also been turning down open borders, this just allows the AI to scout me and see that he likely has a military advantage.
  • I didn't settle many cities (only one in fact, the rest of my cities were captured). Eventually the AI settled near me, and this did cause our relations to sour, but at least it put it off.
  • I've never given money to other civs, but I have occasionally given them free resources if they ask for them nicely.
  • Research agreements are good, but be careful that you don't help one AI get too big of an advantage. I try to make agreements with the civs that have a lower score.

These are the points you should consider if you want to improve the relations:

  • The first and most important factor is the respect of your standing army. And to gain that you have to build it up and show it. Which means if you don't have an open borders agreement don't hide your army deep inside - parade it near the borders. Still better your borders rather then their's if you want to avoid some unnecessary questions.
  • Second most important factor on my experience is the friction caused by close borders. So if you want to avoid conflict don't settle near other Civs. On the other hand if they start to settle near you apparently there is just one thing to do - prepare for war!! (and fast).
  • Giving cities. Apparently AI values cities a lot. Some people even call it an exploit - capturing cities and selling them to other Civs for loads of money. So giving up cities should definetely improve your relations, but it obviously just buys you some time and not solves the problem.
  • Giving money. I don't have much of experience myself, but I imagine we must be talking about hundreds and even thousands of gold, probably depending on your and civ in question wealth.

Now I don't have an idea of how the other agreements actually affect your relations, but I can tell quite well how they can be used as the indicators what your relations are at the moment.

  • Pact of cooperation. If they go for it that means that your relations are really good. And if they cancel it it's the first sign that they are souring.
  • Fair trade. One luxury item for another one item is the sign of good relations. If a Civ wants several items for just one it means they hate you. On the other hand if they are ready to give several items for just one it means they fear you.
  • Pact of secrecy against someone. If a Civ is about neutral about you they won't sign up for cooperation but still might sign a pact of secrecy. And if they cancel that it means your relations are really going on negative side.
  • Research agreements. On my experience even quite pissed Civs will sign up for a research agreement. So I think that is the least factor describing your relations.

I agree with bwarner's excellent answer, just another thing about open borders: I usually decline open border agreements because

  • I'm afraid of a surprise attack
  • I'm annoyed when troops of other civilizations stand in the way of my workers
  • I often expand so that whole areas of land require passing through my territory to access, so by declining open borders I keep those areas for my future cities

However, in the few instances where I do approve of open borders, I've noticed the AI being friendlier towards me. So I do think open borders make you friendlier with other civilizations.