Religion is an attempt to formally codify an ideology such that the people who would eventually have contact with it will redefine which paths they consider valid to live their lives by.
Religion and magic
The more an organized religion considers belief in magic to be superstitious or heretical, the less it will consider its own rituals and practices to involve magic. Instead, a distinction is made in such cases between the manifestation of divine power through officially sanctioned rites (e.g.—prayer, sacraments), and the practice of magic. It becomes a question of authorization. Magic, in the doctrines of such religions, constitutes either folly, an unauthorized attempt to use powers reserved to the deity and its agents, or the invocation of evil or taboo entities or forces.
On the other hand, the more an organized religion sanctions belief in and the practice of magic, the more likely its own rituals will be considered magical. In such cases, magical practice is often considered a connection to, and a means of interaction with, the divine.
Generally speaking, however, most organized religions establish clear doctrines as to the forms and symbols that are acceptable in magical practice, and often issue sanctions against those that violate or fail to conform to those standards. Often, dire consequences are predicted for those who do not adhere to the prescribed ritual forms.
Folk magic has rather mundane and practical aims in mind, and generally has had little problem adapting the religious forms and symbols of the day in the effort to maintain social acceptance. Only where magic itself is taboo is folk magic proscribed.
In cases where magic and religion coexist, there is still a distinction between the social rites officiated by clergy, and the practices conducted by individuals.
Mysticism and magic
In the syncretic hotbed of Alexandria around the time of Philo, the Greek papyri were developed containing a hybrid of Greek, Gnostic, and Egyptian symbols (Hermes-Thoth-Moses, and Set-Typhon). This work formed the basis of much of what later became the Western esoteric tradition. Conventional religion was seen as a necessary structure for ordinary people, but for the mystic, it was little more than a patina concealing the deeper mysteries, and magic was the way to connecting with a reality deeper than God, at least as conventionally understood. The experience was highly individualistic, and the secrets could not be taught. This tradition was elaborated upon and amplified in Medieval Europe by Jewish and Christian mystics, energized by the rise of neo-Aristotlean views as a consequence of the influx of scholarly material brought back to Europe from the Crusades. Hence Kabalah, alchemy, Eliphas Levi, John Dee, and all the rest.
In Eastern traditions, mystics generally view the gods and goddesses as anthropomorphizations of various principles and mystical concepts, and they typically look down upon those who believe that such beings actually exist.
There has always been a certain tension between organized religion and the mystic traditions, due primarily to the fact that organized religion is about social adherence to an established moral order, and the mystic tradition has always emphasized individual exploration and Gnosis. One reason that mystics have been tolerated at all has been the exclusive nature of such movements. Membership is rather restricted, which limits the threat they may pose to the established order. Another is the role that such mystics have played in actually shaping the doctrines that later become mainstream.