When I hear the word essay (which is what I hear 作文 most often translated as), I imagine writing on a topic in which you form an argumentative stance (a thesis) and then support that with facts (body paragraphs) then summarize your work. Is that what the word 作文 conveys, or is it just a piece of writing that is opinionated, rather than a formal essay you would write on Shakespeare or why cigarettes should or should not be banned or something? In other words, is it more of a series of paragraphs, or is it akin to a paper with argumentative rigour or structure akin to a formal English essay you would write in High School or University(unless specified)? Does it depend on what the topic is? Whenever I hear this word it is very confusing because I am always told it means something like "essay" in English.
作文 is closer to "composition", and it primarily refers to writing assignments such as those given as homework to elementary school or middle school students. The subject is not limited to social issues; it can also be about your family, favorite sports, personal dreams, what you ate yesterday, and various other casual topics. In many cases, the focus is on writing sentences with proper grammar and spelling, while the appropriateness or uniqueness of the content is considered less important. A 作文 doesn't have to be opinionated or highly logical.
After progressing to high school, the term 作文 becomes uncommon, although 英作文 (the English version of 作文 assigned by an English teacher) remains common. When mature adults say 作文する or 作文を書く on purpose, it may even have a sarcastic or joking tone, implying a piece of writing that lacks substance or that doesn't require accuracy in its content. For example:「報告書は適当に作文して提出しておけ、どうせ誰も読まないよ」("Just throw together the report and submit it; nobody's gonna read it anyway").
The academic-style essay as you described, such as those assigned to high school or college students, is referred to as 小論文. The essay as an art form, where a professional author's personal opinions or experiences are conveyed through refined prose, is called 随筆 or エッセイ.
作文 often means "a paragraph of literature homework/document" or "a piece of literature", depending on the context.
It can mean an essay that a professor at your university has given you as a homework assignment.
But an essay isn't usually written in a literary style (like Shakespeare's Romeo and Juliet), rather it's often written in a normal or scientific style (clear logical explanations, almost no metaphors or beating around the bush and giving your professor a headache).
I hope this helps to clear up your confusion.