Allah’s (swt) affection for His servants
Love is an existential relationship in which one of its most important causes is the love of oneself that can be found in everyone. Even Allah (swt) is aware of His own essence and attributes and loves Himself because of them, as He does His creations, because they are all His doings and actions that stem from His essence.
The affection Allah (swt) has for His servants isn't the normal affection that people might think and are acquainted with because what such affection necessitates is for Allah (swt) to undergo some certain feelings and react to things, and Allah (swt) is too high and faultless to be such. So what Allah’s (swt) love comes from, is His love for Himself, as we are His actions and doings. What is meant by Allah (swt) loving Himself is that He comprehends and understands goodness. It is Allah’s (swt) awareness to His own beauty that makes Him show affection, and since it is His essence that He is aware of and completely comprehends that makes Him beloved (so from one perspective He loves, and from another perspective He is beloved). Also, since His actions aren't separate from His essence and are one with it, they are also loved by Him, meaning that He loves His actions, and since His creations are the results of His actions, He has affection for them as well. 
Therefore, all things in this world are loved by Allah (swt) the Almighty. What we are asking of here though, is His affection for man in its special meaning, as will be clarified as we go on with this article.
In order to find out how we can be subject to Allah's (swt) affection and what the way is to reach this rank, it is necessary to first get familiar with those who Allah (swt) is affectionate towards, so that we can learn from them what they did to reach such a rank so that we can take the same steps as well.
The Quran speaks of individuals who Allah (swt) was affectionate towards, namely:
Allah is Affectionate to those who are Patient and steadfast 
Allah indeed loves the God wary 
Indeed Allah loves those who trust in Him 
Indeed Allah loves the penitent and He loves those who keep clean 
Allah Is Affectionate to the good-doers 
Indeed Allah loves those who fight in His way 
Allah is Affectionate to those who judge according to justice 
These were some of those whom Allah (swt) loves.
What it means for Allah (swt) to love His servant is that He removes the veils that cover his heart so that he can see Allah (swt) with his heart’s eyes, enabling him to reach ranks of closeness to Him. This decision on Allah’s (swt) behalf is what is considered His love towards him. In other words, Allah’s (swt) love for His servant is the cleansing of his heart from everything other than Him and from all obstacles that stand between him and Allah (swt). 
The urafa (those who have reached high levels of nearness to Allah (swt)) believe that Allah’s (swt) affection for his servants has two forms:
1- Initial affection out of favor and grace which Allah (swt) grants to some of His servants, empowering them to achieve obedience of their lord.
2- A secondary or deserved affection that is the fruit of obedience and obtaining praiseworthy attributes that Allah (swt) likes and this is accomplished through one of two ways:
a) Qurbul-Nawafil (Nearness through mustahabb acts)
b) Qurbul-Fara’idh (Nearness through wajib acts) 
Clearly, one of the signs of Allah’s (swt) love for a servant is that he/she succeeds in obeying the lord by performing wajib and refraining from haram acts.
Ways of obtaining Allah’s love
1- The first step in achieving such a matter is to purify the heart of the love of this world and all things attached to it in an effort to reach Allah (swt). Such can never be accomplished unless one pushes the love of everything other than Allah (swt) out of the heart; the reason being that man’s heart is like a container that doesn’t have the capacity of accepting anything unless it has been emptied of all other contents. At the same time, Allah (swt) hasn’t given anyone two hearts (so that one can encompass all worldly desires and the other contains the love of Allah (swt)). The prophet of Islam (pbuh) says:
The love of this life and world and the love of Allah (swt) cannot gather in one heart together [at the same time]).
Clearly, if Allah (swt) is loved by a person, Allah (swt) will also love him.
2- Patience and steadfastness during hardship, bitter times, tragedies, etc. is another way of getting nearer to Allah (swt) which the Quran and Ahlul-Bayt have emphasized on dramatically. This is why the urafa have considered those who bear the virtue of patience to be loved by Allah (swt). 
3- Following and obeying the prophet (pbuh) because of the verse of:
(Say, if you love Allah (swt), follow me [the prophet] and Allah (swt) will love you too)
is also another way of reaching nearness.
In clarification of the relationship between obeying the prophet (pbuh) and the nearness of Allah (swt), Ibn Arabi says:
“The reason why Allah (swt) considers the obedience of the prophet (pbuh) a means of drawing attention and love is that he is the reflection and manifestation of Allah (swt) in the “mirror” of this world.”
Therefore, from Ibn Arabi’s point of view, no one can be loved by Allah (swt) unless he follows the prophet (pbuh), because the prophet (pbuh) is also in complete obedience of Allah (swt). So, the key in drawing Allah’s (swt) love both in the prophet (pbuh) and in his followers is obedience. It is obedience that makes the difference. 
4- Jihad and fighting in the way of Allah (swt) is yet another way of drawing Allah’s (swt) love. Those who struggle and fight in the way of Allah (swt) are truly loved by Him and Allah (swt) gives them His very special attention; because they have sacrificed their most dearest things for His and Islam’s sake. They are present in all battles between good and evil, whether it is in physical battles or cultural ones against the enemies of Islam. They are like dams that stand in the way of Shaytan and anything that wants to harm Islam; they don’t allow Allah’s (swt) religion to be threatened, and as a result of all these efforts and sacrifice, Allah (swt) loves them.
5- Tawbah or repentance; meaning to be sorry and regretful regarding one’s sins and moving towards and taking refuge in Allah’s (swt) endless grace and mercy, also draws Allah’s (swt) affection. Repentance makes the servant of Allah (swt) draw closer to Him, opening the doors of mercy to him. When one repents, he becomes Allah’s (swt) friend and loved one.
The author of Tafsir Rahnama says:
“One of the reasons why the love of Allah (swt) towards the righteous and those who repent has been pointed to is to encourage them to repent and be righteous. 
6- Those who donate and give charity: Giving charity is also a means of drawing Allah's (swt) love. In reality, giving charity shows one’s gratitude regarding the divine blessings he/she has and is a form of obedience as well. 
7- Faith and good deeds are another way as well, because divine rewards and salvation are all contingent upon faith and good deeds. Therefore, faith that isn't accompanied by good deeds will not entail divine love and salvation.
8- Generosity and kindness also have an effect on drawing Allah's (swt) affection. 
9- Purity: After expressing some forms of worship, such as wudhu, tayammum and ghusl, Allah (swt) discloses the reasoning behind them, which is that He wants us to become pure:
Allah does not desire to put you to hardship, but He desires to purify you. 
Therefore, all commands that Allah (swt) issues, regardless of what the subject of those commands are, are issued for the purpose of purifying our hearts and souls. 
Ayatullah Javadi Amoli says:
“When it is said that Allah (swt) loves those who trust in Him, or those who act justly with others and so on, what the truth of the matter is that He loves the trust itself and loves justice and righteousness, hence the love for those who possess these virtues. 
Of course there are always chances that one who trusts in Allah (swt) or possesses the virtue of patience also possesses bad attributes that Allah (swt) doesn’t like, nevertheless, his good attributes will remain loved by Allah (swt). Nevertheless, one can transcend such a degree (where Allah (swt) only loves his attributes instead of the person himself) and reach a level in which Allah (swt) loves the person himself instead of only his attributes, and that is by achieving all levels of perfection and not wanting anything but Allah (swt) himself. When such takes place, one’s essence is beloved to Allah (swt), and his attributes are loved subsequently. 
When this happens and Allah (swt) has affection for a person himself, not just for his attributes, the whole universe will have affection for that person, because the universe follows Allah (swt) in His will. This rank is one of the highest one can achieve. The significance of Allah's (swt) affection for a person far surpasses the significance of one’s affection for Allah (swt). 
If Allah (swt) is affectionate towards somenone, his actions will be Allah's (swt) actions, his words will be Allah’s (swt), etc. and traits of Allah (swt) will manifest in him. In the famous hadith of “Qurbul-Nawafil”, the prophet of Islam (pbuh) says that Allah (swt) says:
Nothing brings one closer to me than performing what I have obliged him to do, and he continues to draw nearer to me through mustahabb acts until I love him; if he ever reaches a point where I truly love him, I will become his tongue that he speaks with out of my special grace, I will become his sight in which he sees with out of my special grace.
it is on this basis that the speech of the believer who is loved by Allah (swt) becomes like it is the speech of Allah (swt) himself.
Also, when Allah (swt) becomes affectionate towards someone, He falls in love with him, as a hadith by the prophet (pbuh) says:
When Allah (swt) becomes affectionate of someone, He makes that person fall in love with Him, in addition to Allah (swt) loving him as well. Then Allah (swt) goes on to say: “O servant of mine! You are in love with me, and I also love you, whether you like it or not!”
To sum it all up, there are various ways of reaching a point in which Allah (swt) becomes affectionate towards one, which can all be summarized in doing what Allah (swt) has obliged and refraining from what he has prohibited.