Variables just store data. How the data gets in there is up to your SSIS package. In many cases the variable's value is set when it is created in the designer (see Variables window). However if you need it to be dynamic then you need to add a step in your SSIS package to set the variable as discussed here.
In cases where your variable(s) need to be set based upon a query that runs then you generally set up the query to run as normal (data flow task) and then you define a variable within the scope and set its value to an expression that pulls the current value from the query. There is no need for a script task for this. After that the variable will retain its value until it is set to something else or goes out of scope.
As an example a common thing to do is enumerate the rows of a query using a Foreach task. Each time through the loop you want to assign a value from the current row to a variable. To do that you'd go to the Foreach task and it has the option of defining variables. You define your variable and you set its value to be an expression that is evaluated from the current row's value (available via the Foreach) task. Then each time through the loop SSIS will update the variable to the current value as defined by the row. An example can be seen here.
For static variable values just define the variable and set its value directly.