Affinity

Affinity is a companion gameplay mechanic in Fallout 4. Companions can develop personal connections to your character and some may ask for your help with quests of their own. Each companion has a distinct personality and values, actions or dialogue that please one companion may displease another.

Raising the affinity value grants additional dialogue into their history or the possibility of romance. Reaching maximum affinity with a companion will unlock a special perk. The affinity value is a hidden statistic, meaning it is not possible to check your progress without console commands.

Scale
Typically, all companions begin at an affinity value of 0. If the affinity value reaches 500, they will "admire" you and often reveal new dialogue. Raising their affinity to the maximum value of 1000 will result in them "idolizing" you, at which point you are granted their companion perk.

Lowering the affinity to a sufficient negative value will displease the companion, and they will issue a single warning to you, asking you to change your ways. If the affinity value drops further, they will become angry and refuse to travel with you. This decision is permanent.

For most companions, new dialogue will be triggered when the affinity value reaches 250, 500, 750 and 1000.

Reactions
There are four different reactions a companion can have towards your actions/dialogue that will affect their affinity value:

Some actions, such as modifying weapons, picking locks, and entering power armor will raise affinity with certain companions and can be repeated indefinitely. Performing a single one of these actions within range of multiple characters who respond to said actions will trigger the respective increases or decreases with all of them. However, these responses have a cooldown period, meaning it is not possible to perform the same action several times in a row to gain/lose affinity. (For example, entering power armor will trigger only one instance of approval from Paladin Danse and/or X6-88 until sufficient time has elapsed.)

It is however possible to perform different actions back-to-back and receive two or more separate instances of approval. (For example, picking a lock and then hacking a terminal will grant two separate approvals from Deacon.) Similarly, in a conversation with an NPC, each dialogue choice counts as an action unto itself, independent of the rest of the overall dialogue, so it is possible to obtain multiple likes and loves in the same set of dialogue with the same NPC. (For example, in Order Up, you can gain two likes from Piper by saying "You're both wrong" to Wolfgang, then agreeing to talk down Trudy.) For a more-detailed list of what specifically pleases or displeases a companion, see the respective companion page.

Affinity can be also increased passively by simply having a companion follow you. The increase depends on the current affinity level and will be lower with higher affinity values. The formula for the increase is 40 - 0.033 x affinity value. (i.e. companions with 100 Affinity will gain a 36.7 increase, companions with 250 = +31.75, 500 = +23.5, 750 = +15.25, 990 = +7) This only applies if a quest has been completed since the companion was last chosen. Thus, the method is most effective in the time just after meeting them, with the addition of sending them to the same settlement(s) to be rotated with greater ease between turning in quests.

Romance
Certain companions are potential candidates for romance and throughout the course of their affinity based conversations, the dialogue option to "flirt" will be available. At the highest level of affinity - and final conversation - the "romance" option will be available. Both options are speech checks. Successfully flirting will reduce the difficulty of the final romance check and successfully passing that will make the companion a love interest. This is result in them addressing you in a more affectionate manner. Also, sleeping in a bed with them nearby will also grant the Lover's Embrace perk.

Companion romances are not mutually exclusive; if a new companion is romanced, previously romanced partners will still confer the benefits of the Lover's Embrace. (However, some romanced partners will "dislike" flirting in their presence.)

Companion perks
Those perks are acquired by reaching the maximum level of affinity with a companion. The perks obtained this way are permanently obtained, whether you keep travelling with the associated companion or not.

Exploits
The following actions not only increase the affinity of the listed companions, but can be repeated after an internal "cooldown" has reset. Saving after performing the action and reloading the file will reset any triggered cooldowns, allowing large amounts of affinity to be produced in a relatively short period of time.

Weapon modding

 * Codsworth and Danse love this. Preston and X6-88 like this.  It is easily repeated with the "None" attachment, which simply removes an existing modification and costs nothing to produce.  They can all be affected by being within range of the weapon workbench simultaneously, making it much easier to raise all four companions' affinity.

Armor modding

 * Codsworth and Danse love this. X6-88 likes this.  See Weapon modding.

Entering power armor

 * Danse and X6-88 like this. Note that Strong dislikes this, so he should not be in the same settlement or general location while performing this.

Stealing

 * MacCready likes this. Can be repeated by placing an item in a container owned by someone else and taking it back out, preferably in an area you can close from prying eyes such as a hotel room in Goodneighbor.

Lockpicking
Such safes can be found in:
 * Cait, Deacon, and Piper like this. MacCready likes this, but only to locks owned by others, whereas Strong dislikes picking those locks.
 * Can be repeated by using a safe that has a terminal attached to it, opening the safe then using the terminal to lock it again.
 * Sanctuary (unless scrapped)
 * Fiddler's Green Trailer Estates
 * Medford Memorial Hospital (advanced)
 * Back Street Apparel (advanced 1st floor)
 * Hubris Comics (Grognak's axe case)
 * Greenetech Genetics (expert 7th floor)
 * Super-Duper Mart (advanced)
 * Ticonderoga (expert 1st floor near exit terminal)

Using chems

 * Hancock likes this, as does Cait before her companion quest. Afterwards, she dislikes it.

Using alcohol

 * Cait likes this before her companion quest. Afterwards, she is indifferent.

Nudity

 * Hancock and Cait like this. Can be repeated by fast traveling to settlements and other safe areas with all armor/clothing unequipped.

Cannibalism

 * Strong likes this. (Requires Cannibal perk)

Healing Dogmeat

 * Everyone except Strong and X6-88 like this. Repeating this will likely require deliberately wounding Dogmeat in a settlement with the character(s) in question nearby, as you can only have one companion out at a time.

With the exception of healing Dogmeat, Curie and Nick Valentine have no repeatable/farmable actions, so the approval gained from accepting and completing quests should be prioritized to them. Nick likes hacking terminals, but the game has a finite number of them (but still more than enough to max his Affinity as long as his companion quest is completed before running out).